Incement column in VBA

wilest

New Member
Joined
Apr 29, 2011
Messages
12
Hi
I desperately need help. I want my macro to:
select A1:C68 and cut it and paste it on another place and the
select D1:F68 and cut it and paste it on another place etc etc
Please help!

Code:
    Dim i As Integer
    var1col = 1
    var2col = 3
    var3 = 1
    For i = 1 To 100
    Windows("Book2").Activate
    Range(var1col & "1" & ":" & "68" & var2col).Select
    Selection.Cut
    Windows("results.xlsx").Activate
    Range("E" & var3).Select
    ActiveSheet.Paste
    var1col = var1col + 3
    var2col = var2col + 3
    var3 = var3 + 68
    Next i

 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi and welcome to the board!!!
This is untested, but shouuld be close
Code:
Sub CopyMe()
Dim WB1 As Workbook
Dim WB2 As Workbook
Set WB1 = Windows("Book2")
Set WB2 = Windows("results.xlsx")
Dim LR As Long
Dim LC As Long
Dim i As Long
LC = WB1.Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LC Step 3
WB1.Sheets("Sheet1").Cells(1, i).Resize(68, 3).Copy
LR = WB2.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
WB2.Sheets("Sheet1").Cells(LR + 1, "A").Paste
Next i
End Sub

lenze
 
Upvote 0
Thank you but it is complaining it gives a run time error, type mismatch and if i click debug it complains about

Set WB1 = Windows("Book2")
 
Upvote 0
OK, then try this
Code:
Sub CopyMe()
Dim LR As Long
Dim LC As Long
Dim i As Long
LC = Windows("Book2").Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LC Step 3
Windows("Book2").Sheets("Sheet1").Cells(1, i).Resize(68, 3).Copy
LR = Windows("results.xlsx").Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Windows("results.xlsx").Sheets("Sheet1").Cells(LR + 1, "A").Paste
Next i
End Sub

lenze
 
Upvote 0
You know his code will only work if you have a Book2, right? Book1 won't work, neither will anyhing other than Book2.
 
Upvote 0
Hi All
I understand that I must have a book2 active or open for the macro to work, but it still gives me errors on the following code:

Code:
LC = Windows("Book2").Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column

Any other ideas? Thanks for the great forum!
 
Upvote 0
If I understand correctly you could try this - change worksheet/workbook names as needed.
Code:
Option Explicit
 
Sub MoveData()
Dim wbSrc As Workbook
Dim wbDst As Workbook
Dim wsSrc As Worksheet
Dim wsDst As Worksheet
Dim rngSrc As Range
Dim rngDst As Range

    Set wbSrc = ThisWorkbook
    
    Set wsSrc = wbSrc.Worksheets("Sheet1")
    
    
    Set rngSrc = wsSrc.Range("A1:C68")
    
    Set wbDst = Workbooks("OtherWorkbook.xls")
    
    Set wsDst = wbDst.Worksheets("Sheet2")
    Set rngDst = wsDst.Range("E1")
        
    While rngSrc.Cells(1, 1).Value <> ""
    
        rngSrc.Copy rngDst
        
        rngSrc.Clear
        
        Set rngSrc = rngSrc.Offset(, 3)
        
        Set rngDst = rngDst.Offset(68)
        
    Wend
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,519
Messages
6,179,263
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top