VBA copy paste codes does not pasting anything - excel
Can someone please let me know why my code is not pasting anything from my source data to my destination file?
The objectives of this code are to select rows that satisfy certain criteria, copy-pastes it into another workbook, The code is shown below:
Sub Copy_Source_LRE()
Dim LastRow As Integer, i As Integer, erow As Integer
Workbooks.Open _
"C:\Users\sjiang\OneDrive - Canada Pension Plan Investment Board\Desktop\IOA Exposure\AAPAF_strategy_loadings_2019-04-01_2020-04-01 (2).csv"
Worksheets("AAPAF_strategy_loadings_2019-04").Activate
Set sht = ActiveSheet
'Workbooks("AAPAF_strategy_loadings_2019-04-01_2020-04-01 (2).csv").Sheets("AAPAF_strategy_loadings_2019-04").Activate
LastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row
For i = 2 To LastRow
For Each d In Array("4/1/2019", "5/1/2019", "6/3/2019", "7/1/2019", "8/1/2019", "9/2/2019", _
"10/1/2019", "11/1/2019", "12/2/2019", "1/2/2020", "2/3/2020", "3/2/2020")
If Cells(i, 2) = d And Cells(i, 3) = "Real Estate" And Cells(i, 4) = "Listed Real Estate" And Cells(i, 5) = "AAPAF_SA" Then
Range(Cells(i, 2), Cells(i, 12)).Select
Selection.Copy
Workbooks.Open _
"C:\Users\sjiang\OneDrive - Canada Pension Plan Investment Board\Desktop\IOA Exposure\pull data.xlsm"
Worksheets("Sheet1").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
'ActiveWorkbook.Close
End If
Next d
Next i
Application.CutCopyMode = False
End Sub
This is a really easy and basic way that I use all the time to copy data into new workbooks. In this example I'm copying a named range called "MasterData" into a new blank workbook. Then I save that new book with a password and re-activate the current workbook.
Dim newfilename As String
newfilename = "/Users/" & userName & "/Desktop/savedWorkbook.xlsx"
Dim NewBook As Workbook
Set NewBook = Workbooks.Add
ThisWorkbook.Activate
Sheets("Datasheet").Select
Range("MasterData").Copy
NewBook.Activate
NewBook.Sheets(1).Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
NewBook.SaveAs Filename:=newfilename, Password:="examplepassword", AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
NewBook.Close (True)
ThisWorkbook.Activate
I've redone the code for you as the major problem was related to a loop that is not really necessary. The best/fast way to apply those criteria and extract the data is using a filter to apply them, so copy the visible cells without the hidden (unmatching) lines and then open the second file where you need to past info, find next blank line below selection and paste all lines at once.
I'm pasting the code below (with comments) and also saved a zip file with 3 files (code, info, database) that might reflect your working files, link below.
VBS code:
Sub Copy_Source_LRE()
Dim LastRow As Integer, i As Integer, erow As Integer
Workbooks.Open ThisWorkbook.Path & "\" & "Wks1.xlsx" 'change the path and name here
Worksheets(1).Activate
Set sht = ActiveSheet
LastRow = Range("a1").SpecialCells(xlCellTypeLastCell).Row
datar = Range(Cells(LastRow, 12), Cells(1, 1)).Address 'data range
Range(datar).Select
Selection.AutoFilter 'create a filter,then use the criteria you need
ActiveSheet.Range(datar).AutoFilter Field:=2, Criteria1:= _
Array("4/1/2019", "5/1/2019", "6/3/2019", "7/1/2019", "8/1/2019", "9/2/2019", _
"10/1/2019", "11/1/2019", "12/2/2019", "1/2/2020", "2/3/2020", "3/2/2020"), Operator:=xlFilterValues 'your dates array can be update here
ActiveSheet.Range(datar).AutoFilter Field:=3, Criteria1:="Real Estate", Operator:=xlAnd
ActiveSheet.Range(datar).AutoFilter Field:=4, Criteria1:="Listed Real Estate", Operator:=xlAnd
ActiveSheet.Range(datar).AutoFilter Field:=5, Criteria1:="AAPAF_SA", Operator:=xlAnd
Range(datar).Offset(1, 0).Resize(Range(datar).Rows.Count - 1, Range(datar).Columns.Count).Select 'resize selection to remove the header
Selection.SpecialCells(xlCellTypeVisible).Select 'select visible cells only
Selection.Copy
Workbooks.Open ThisWorkbook.Path & "\" & "Wks2.xlsx" 'change the path and name here
Worksheets("Sheet1").Select
Range("A1").End(xlDown).Offset(1, 0).Select 'goes to the last row on column A the goes another one - 1st empty
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close SaveChanges:=True 'close and save your database
Application.CutCopyMode = False
ActiveWorkbook.Close SaveChanges:=False 'close without saving your csv file
End Sub
link to files/code: https://drive.google.com/file/d/1zL_TwclHR4lrNhKB1xODGAmliPHM1r3K/view?usp=sharing
If the solution matches you need please consider as solution. Regards!
Related
To add the excel formulas into VBA Macro with condition
I have recorded some formulas into Macros and they are functioning properly, however I am not able to update them so that they should select the range themselves where the data ends in the last end in Column C. These 3 formulas extracts Date, File Name and Status of Files from Column A. As you see for now the range is e.g. "F3 to F313" where next time if the Data in Column C is up to C500 Range than I have to manually copy and paste the formulas. Is there anyway these 3 formulas should automatically detect the last text cell from Column C and ends there. That would be much helpful. To Extract Date Sub Macro13() 'To Extract Date ActiveCell.FormulaR1C1 = "=extractDate(RC[-1])" Range("D2").Select Selection.Copy Range("D3:D313").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub To Find Status of File Sub Macro15() 'To Find Status of File ActiveCell.FormulaR1C1 = _ "=IFERROR(LOOKUP(2^15,SEARCH({""Feed"",""Feed 1"",""Feed 2""},RC[-3]),{""Feed"",""Feed 1"",""Feed 2""}),""Combine"")" Range("F2").Select Selection.Copy Range("F3:F313").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub To extract File Name Sub Macro17() 'To extract File Name ActiveCell.FormulaR1C1 = _ "=IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABCD - GAMA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ALPHA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9," & _ "0},RC[-2]&""1234567890""))-1))=""ABCD - BETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""DBETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""A"",LEFT(RC[-2]," & _ "MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))="""",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABETA"",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),LEF" & _ "T(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2] & ""1234567890""))-1))))))))" Range("E2").Select Selection.Copy Range("E3:E313").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub
Try this: Sub TestThis() Dim LastRow As Long, ws As Worksheet Set ws = ActiveSheet LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ws.Range("D2:D" & LastRow).FormulaR1C1 = "=extractDate(RC[-1])" ws.Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(LOOKUP(2^15,SEARCH({""Feed"",""Feed 1"",""Feed 2""},RC[-3]),{""Feed"",""Feed 1"",""Feed 2""}),""Combine"")" ws.Range("E2:E" & LastRow).FormulaR1C1 = _ "=IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABCD - GAMA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ALPHA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9," & _ "0},RC[-2]&""1234567890""))-1))=""ABCD - BETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""DBETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""A"",LEFT(RC[-2]," & _ "MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))="""",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABETA"",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),LEF" & _ "T(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2] & ""1234567890""))-1))))))))" End Sub
Format worksheets 5 and on, then copy&paste that info into "Sheet3" with source width and format
I am currently try to make a code that will format sheets 5 and on to module one's code and then have the program copy all the information in each of those newly formatted sheets and paste them into "sheet3" with original width and format. I have tried the "for each" and "integer" functions but can't seem to get 'the program to move past "sheet5". This sub is suppose to go through all of the sheets and and 'format them to my needs: Sub TEST2() Dim ws As Worksheet Dim wsDest As Worksheet Dim LastRow As Long Set wsDest = Sheets("sheet3") For Each ws In ActiveWorkbook.Sheets If ws.Name <> wsDest.Name And _ ws.Name <> "sheet1" And _ ws.Name <> "sheet2" And _ ws.Name <> "sheet4" Then 'code here Columns.Range("A:A,B:B,H:H,I:I").Delete Columns("A").ColumnWidth = 12 Columns("B").ColumnWidth = 17 Columns("C").ColumnWidth = 10 Columns("D").ColumnWidth = 85 Columns("E").ColumnWidth = 17 ActiveSheet.Range("D:D").WrapText = True ActiveSheet.Range("F:F").EntireColumn.Insert ActiveSheet.Range("F1").Formula = "Product ID" LastRow = Cells(Rows.Count, 1).End(xlUp).Row Range("F2:F" & LastRow).Formula = "=$G$2" ActiveSheet.Range("F2").Copy Range("F2:F" & LastRow).PasteSpecial Paste:=xlPasteValues End If Next ws End Sub This sub is meant to go to "sheet5" first and paste it into '"sheet3", than the second half of the sub should start at "sheet6" and go on 'until the end of the work sheets and then copy & paste onto "sheet3" with 'original width. Sub Test1() Dim sht As Worksheet Dim LastRow As Long Dim WS_Count As Integer Dim I As Integer Sheets("Sheet5").Select Application.CutCopyMode = False Application.Run "PERSONAL.XLSB!Module1.Del_move_insert_colmuns" Range("G2").Select ActiveCell.Offset(0, -1).Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToLeft)).Select Application.CutCopyMode = False Selection.Copy Sheets("Sheet3").Select Range("A1").Select ActiveSheet.Paste Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False ActiveSheet.Range("D:D").WrapText = True WS_Count = ActiveWorkbook.Worksheets.Count ' Begin the loop For I = 5 To WS_Count 'code here Sheets("Sheet6").Select Application.Run "PERSONAL.XLSB!Module1.Del_move_insert_colmuns" Application.CutCopyMode = False Range("G2").Select ActiveCell.Offset(0, -1).Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToLeft)).SelectApplication.CutCopyMode = False Selection.Copy Sheets("Sheet3").Select Range("A1").Select 'crtl shift + down Selection.End(xlDown).Select 'moves down one cell to paste ActiveCell.Offset(1, 0).Select ActiveSheet.Paste Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Next I End Sub What im getting right now is it does "sheet5" and "sheet6" fine,but after that doesn't format and on sheet there all i get is a bunch of columns with top labeled as product ID and a bunch of 0's.
A big part of your problem is that most of your code is "assuming" that you are working with a certain worksheet when you're really working with the ActiveSheet. As an example in your TEST2 routine, you're looping through all of the worksheets in the workbook, skipping certain sheets. This part works fine. But when you want to format the other sheets, you're really only working with whatever worksheet is currently active. To fix this, you should make a habit of making sure all of your Worksheet, Range, and Cells reference are always fully qualified. So then your code works like this: ws.Columns.Range("A:A,B:B,H:H,I:I").Delete ws.Columns("A").ColumnWidth = 12 ws.Columns("B").ColumnWidth = 17 ws.Columns("C").ColumnWidth = 10 ws.Columns("D").ColumnWidth = 85 ws.Columns("E").ColumnWidth = 17 ws.Range("D:D").WrapText = True ws.Range("F:F").EntireColumn.Insert ws.Range("F1").Formula = "Product ID" LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ws.Range("F2:F" & LastRow).Formula = "=$G$2" ws.Range("F2").Copy ws.Range("F2:F" & LastRow).PasteSpecial Paste:=xlPasteValues Notice how every single reference is locked to the same worksheet. You can take a shortcut though, by using the With statement. But you must make sure that each reference has the . in front of it to lock it back to the With object, like this: With ws .Columns.Range("A:A,B:B,H:H,I:I").Delete .Columns("A").ColumnWidth = 12 .Columns("B").ColumnWidth = 17 .Columns("C").ColumnWidth = 10 .Columns("D").ColumnWidth = 85 .Columns("E").ColumnWidth = 17 .Range("D:D").WrapText = True .Range("F:F").EntireColumn.Insert .Range("F1").Formula = "Product ID" LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row .Range("F2:F" & LastRow).Formula = "=$G$2" .Range("F2").Copy .Range("F2:F" & LastRow).PasteSpecial Paste:=xlPasteValues End With For the rest of your code, you can make improvements by avoiding the use of Select and Activate. Consider also the tips discussed in this article that will give you excellent guidance.
Excel Macro to Concatenate first and last name sometimes fails
I am a Visual Basic newbie. From hints on the web, I pieced together an Excel macro that does several things, including concatenating first and last name, in a loop, to make a new column with those joined. Half the time it works great, half the time I end up with no space between the first and last name. (In those cases, closing, re-opening, and re-running almost always works.) Is this a timing issue? I'll put in the whole macro but it's the Do While loop near the top that I think is the problem. Thanks for any help. Sub WholeThing() ' ' WholeThing Macro Application.ScreenUpdating = False ActiveSheet.Name = "original" Rows("1:1").Delete Shift:=xlUp Do While ActiveCell <> "" 'Loops until the active cell is blank. ActiveCell.Offset(0, 0).FormulaR1C1 = _ ActiveCell.Offset(0, 1) & " " & ActiveCell.Offset(0, 2) ActiveCell.Offset(1, 0).Select Loop Application.Wait (Now + TimeValue("0:00:02")) Sheets.Add After:=Sheets(Sheets.Count) Sheets.Add After:=Sheets(Sheets.Count) Sheets.Add After:=Sheets(Sheets.Count) Sheets.Add After:=Sheets(Sheets.Count) Sheets.Add After:=Sheets(Sheets.Count) Sheets.Add After:=Sheets(Sheets.Count) Sheets("Original").Activate ActiveWindow.WindowState = xlNormal Application.CutCopyMode = False Application.DisplayAlerts = False Range("A1").Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy ActiveSheet.Paste Destination:=Worksheets("Sheet6").Range("A1") Range("D1").Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy ActiveSheet.Paste Destination:=Worksheets("Sheet6").Range("B1") Sheets("Original").Activate ActiveWindow.WindowState = xlNormal Application.CutCopyMode = False Application.DisplayAlerts = True Columns("Y:Y").Copy ActiveSheet.Paste Destination:=Worksheets("Sheet2").Range("A1") Columns("Z:Z").Copy ActiveSheet.Paste Destination:=Worksheets("Sheet3").Range("A1") Columns("AA:AA").Copy ActiveSheet.Paste Destination:=Worksheets("Sheet4").Range("A1") Columns("AB:AB").Copy ActiveSheet.Paste Destination:=Worksheets("Sheet5").Range("A1") Application.DisplayAlerts = False Sheets("Sheet5").Activate ActiveWorkbook.SaveAs Filename:="Y:\Addrs_DL", FileFormat:=xlCSV, _ CreateBackup:=False Sheets("Sheet4").Activate ActiveWorkbook.SaveAs Filename:="Y:\Addrs_D", FileFormat:=xlCSV, _ CreateBackup:=False Sheets("Sheet3").Activate ActiveWorkbook.SaveAs Filename:="Y:\Addrs_SL", FileFormat:=xlCSV, _ CreateBackup:=False Sheets("Sheet2").Activate ActiveWorkbook.SaveAs Filename:="Y:\Addrs_S", FileFormat:=xlCSV, _ CreateBackup:=False Sheets("Sheet6").Activate ChDir "Y:\" Application.ScreenUpdating = True ActiveWorkbook.SaveAs Filename:="Y:\NAME-ADR.CSV", FileFormat:=xlCSV, _ CreateBackup:=False ' Application.Quit ' Application.ActiveWindow.Close SaveChanges:=False ' ActiveWorkbook.Close SaveChanges:=False End Sub
By not using ActiveCell and working with your range directly, you can make your code more stable and more reliable. Consider something like this (see notes about assumptions on range and cell references). Dim ws as Worksheet Set ws = ThisWorkbook.Sheets("original") With ws Dim lRow as Long lRow = .Range("B" & .Rows.Count).End(xlup).Row 'assumes first name in column B 'assumes concatenated name goes in column A, starting at row 1 (and the first and last name are in B and C, respectively .Range("A1:A" & lRow).FormulaR1C1 = "=RC[1] & "" "" & RC[2]" 'if you want to copy as values you can use this .Range("A1:A" & lRow).Value = .Range("A1:A" & lRow).Value End With You can also work with the same principles of working directly with the object later on in your code, like this: 'lRow would be the last row of data in the column (assumes same row for each column, based on dataset) ws.Range("Y1:Y" & lRow).Copy Worksheets("Sheet2").Range("A1") Doing this will save a lot of processing time as copying entire columns is very inefficient if it's not truly needed.
To do the concatenate, I had first to use this to get the number of the last row: Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With That enabled this loop to do the concatenation: Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("original") With ws For i = 1 To LastRow Cells(i, 1) = Cells(i, 2) & " " & Cells(i, 3) Next i Then, for the second block (the "With ws" being still in effect): Sheets("Original").Activate Range("Y1:Y" & LastRow).Copy Worksheets("Sheet2").Range("A1")
How do I copy a worksheet from one workbook into another new workbook with just that worksheet in (with pastevalues)?
I am trying to create a macro that takes a worksheet from a workbook, and then saves that worksheet as a single workbook but with all cells as values rather than formulas. The bit I am struggling with is the paste values section. This is what I have currently: ThisWorkbook.Sheets("Tickets (1-48)").Copy With ActiveWorkbook For ColNr = 8 To 120 If Cells(RowNr, ColNr - 1) = "0" Then TempValue = Cells(RowNr, ColNr - 1) If TempValue = "0" Then For i = 0 To 9 Cells(RowNr, ColNr - 1).Select Cells(RowNr, ColNr - 1).EntireColumn.Delete Next i ColNr = ColNr - 1 Else End If Else End If Next ColNr .SaveAs strpath & "\" & "Retail " & strFilename & "(1-48)" & ".xls" .Close 0 End With Ignore the section in the middle. Thanks.
I'm not sure what your code is doing but you can adapt it to this. Here is the general code that a recorded macro will show you. 'Selects everything on the current sheet and copies it Cells.Select Selection.Copy 'Add a new workbook. 'Adding a new workbook makes it the active workbook so you can paste to it. Workbooks.Add 'Paste the date using Paste:=xlPasteValues Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Range("C8").Select
Cant write to cell
I am trying to get data from every other sheet in the workbook and paste it into a master sheet (Sheet1), but when I run my code nothing happens, Can someone tell me why this is? Dim wb As Workbook Dim ws As Worksheet Set wb = ActiveWorkbook wb.Sheets("Sheet1").Cells(1, 1).Text = "Started" i = 1 'cells is row,col For Each objWorksheet In wb.Worksheets 'ws.Cells(1, i).Value = objWorksheet.Name 'objWorksheet.Activate 'ws = wb.ActiveSheet doJStuff i, objWorksheet i = i + 1 Next wb.Sheets("Sheet1").Cells(1, 1).Text = "DONE" End Sub Sub doJStuff(row, ws) ws.Select Range("A2").Select Application.CutCopyMode = False Selection.Copy Sheets("Sheet1").Select ActiveSheet.Cells(row, 1).Select ActiveSheet.Paste 'end paste name 'copy post history and transpose into row 2 on sheet1 ws.Select Range("H2:H30").Select Application.CutCopyMode = False Selection.Copy Sheets("Sheet1").Select ActiveSheet.Cells(row, 2).Select Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=True 'end post history End Sub
You've got a number of problems with your code. First of all, avoid using Select wherever possible. Second, you are not properly assigning variables. You should put Option Explicit on the top of the module and then make sure you've assigned things correctly. As for the actualy code, when you are copying/pasting the H2:H30 range into your first sheet you are going to only end up getting the first value in the range for every sheet except the last because you are pasting 28 rows but only incrementing the destination row by one. I didn't fix that but it's worth pointing out. I also left in your comments though they don't make much sense. Without knowing what you are trying to do, I've only cleaned up some of your code but it probably still won't work exactly right. Sub YourSub() Dim wb As Workbook Dim wksht As Worksheet Dim i As Integer Set wb = ActiveWorkbook wb.Sheets("Sheet1").Cells(1, 1).Text = "Started" i = 1 'cells is row,col For Each wksht In Worksheets 'ws.Cells(1, i).Value = objWorksheet.Name 'objWorksheet.Activate 'ws = wb.ActiveSheet doJStuff i, wksht i = i + 1 Next wb.Sheets("Sheet1").Cells(1, 1).Text = "DONE" End Sub Sub doJStuff(row As Integer, ws As Worksheet) ws.Range("A2").Copy Sheets("Sheet1").Cells(row, 1).PasteSpecial 'end paste name 'copy post history and transpose into row 2 on sheet1 ws.Range("H2:H30").Copy Sheets("Sheet1").Cells(row, 2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=True 'end post history End Sub