Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

how do I move a range of data that will be randomly positioned to another range

0

I have to move a table of date that can be randomly placed on a worksheet to a set position on another worksheet. I have solved how to locate the edges of the data I need and if this line worked:

Sheets("results").Range(Cells(X + 1, 2), Cells(X + 1, 12)) = Range((TopCell.Offset(X - 1, -3)), (TopCell.Offset(X - 1, 7)))

It would do what I need to do. I have checked and both range definitions seem valid.

I appreciate the help. In my last question I was pointed to https://docs.microsoft.com/en-us/office/vba/api/Excel.Range and that helped immensely.

Answer
Discuss

Answers

0

The basic code you might employ is shown below. It's the same that you would use with the Copy command instead of Cut.

Sheet2.Range("A2:A6").Cut Destination:=Sheet1.Cells(1, 1)

This looks simple, and it is, but there are a few points you need to know. For one, I use the CodeName to identify the sheets. By default, each worksheet will have two names, listed in VBE's project explorer like "Sheet1 (Sheet1)". Originally, both names are the same. In the properties you find them repeated as (Name) and Name. For reasons best (or only) known to Microsoft Name corresponds to (Name) and (Name) to Name. You can change the Name in the properties and that will give the tab a new name. You can also change the (Name) and that has no effect you can see. (Name) is the CodeName which is a property of the worksheet object. Change the Name to "My Sheet". Now you can assign this sheet to a variable:-

Dim Ws As Worksheet
Set Ws = Worksheets("My Sheet")
Debug.Print Ws.Name, Ws.CodeName

This will print the name ("My Sheet") and the CodeName ("Sheet1"). As you see, the CodeName doesn't change when the tab is renamed, and that is its use and purpose. In fact, Set Ws = Worksheets("My Sheet") will create a duplicate of the worksheet object identified by Codename. You can use Sheet1 in exactly the same way as Ws. Excel already prepared the variable for you, and that's how I used it above.

What's the difference between your approach, like, Sheet2.Range("A2:A6") = Sheet1.Range("A2:A6") and mine? To understand this you need to know that Value is the default property of the Range object. The Range object has many properties, of course. So, when you write Range("A2:A6") do you mean the range with all its properties or do you mean the Value of that range? Since your code says nothing Excel will decide.For reasons again known best to Microsoft Excel refuses to decide in this particular case (at least in the current Excel 365) and the code does nothing, not even crash. But the line below will work.

Sheet2.Range("A2:A6").Value = Sheet1.Range("A2:A6").Value

This code is also easier to read. It's clear that the values of one range are copied to another range. Compare that to the Copy/Cut code first above given. There all properties are copied, including formats and formulas, not only the values. (Observe that when you cut or copy =2 + 3 the formula will be moved or copied but when you copy values the Value 5 will appear at the destination).

Further observe that the use of the equal sign (=) requires that the size of source and target range be the same. But when using Cut/Copy and Destination specifying the frist cell of the latter is sufficient.

BTW, do you know the difference between Sheets and Worksheets? If you think the two are the same better think again. Why, for instance, can't you just say

Dim S As Sheet
Set S = Sheets("Sheet1")
??
Discuss

Discussion

Thanks for your answer. One of my issues is that I have to use the cell(1,1) because what I am doing is locating the column that a value I know will be in and then finding the top value in that column. From there I can define the line of data that was the range in the code I shared in my initial question.
       Another issue is that I am a true novice at this (obviously). How I get done what I do Is to google what I am trying to do and then make it fit what I want to have happen. I remain curious why my statement above didn't work. I will add that apparently I don't know the difference between sheets and worksheets even though I thought I did. I thought both were basically the pages of a workbook as defined by the tabs at the bottom. 





         
baddog1016 (rep: 12) Sep 30, '20 at 10:05 pm
here's the code where I find the parameters for the range:
   Dim TopCell As Range
   Dim ThisPos As Range
   Dim R1 As Range
    With Range(Cells(1, 1), Cells(100, 100))
        Set ThisPos = .find(What:="Cosmic Debris", lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
            ThisCol = ThisPos.Column
            ThisRow = ThisPos.Row
    End With
 
For i = 1 To 20
increm = ThisPos.Offset(-i, 0).Value 'increm is cell contents incrementing up from search criteria
 ekp = ThisRow - i  'this increments biteme
 If increm = 0 Then
Set TopCell = Worksheets("results").Cells(ekp, ThisCol)
 Exit For
 End If
baddog1016 (rep: 12) Sep 30, '20 at 10:09 pm
Let's try and stick with the forum rules where one asks a question and anotehr gives the answer. The only thing for "Discussion" is why the answer that was given doesn't solve the problem that was posed. If, for that purpose the question must be made more precise then, for the benefit of future visitors, the question must be made more precise (edit the question).
The Worksheets collection comprises of all worksheets in a workbook whereas the Sheets collection includes all the sheets, including work- pivot- and chart sheets. Obviously, finding a worksheet will be faster to execute when looking among worksheets than when looking through all kinds of sheets. I see no time being saved when I avoid typing 4 characters and get a less efficient code which, in addition, is also less logical to read because the object I declared As Worksheet isn't looked for among Worksheets but among Sheets. It's like looking for a ball among toys. There's nothing wrong with it but it isn't efficient.
Variatus (rep: 4889) Oct 1, '20 at 2:43 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login