VBA macro to delete a row - excel

Hey i just created a macro added headers deleted info and got data formatted
but i noticed that when i ran it for another file
it just deleted the data in that exact cell i now need to
do the same
but delete the row that the phrase sits on
imagine i had a cell a1 in other versions of the document that phrase could be in a2
my macro would only delete whats in A1
the phrase is ZFD
and whatever cell its in i need the macro to delete the entire row that phrase sits on
HELPPPPP
Sub UMR()
'
' UMR Macro
'
'
Range("A1").Select
ActiveCell.FormulaR1C1 = "Transaction_Type"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Meter_Point_Ref"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Actual_Read_Date"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Source"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Reason"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Meter_Serial_Number"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Meter_ROC_Count"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Meter_Read_Verified"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serialNumber"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serial_Number"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Corrector_Uncorrected_Reading"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Corrector_Corrected_Reading"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Corrector_ROC_Count"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Corrector_Usable_IND"
Range("O1").Select
ActiveCell.FormulaR1C1 = "Corrector_Read_Verified"
Range("A17").Select
Selection.ClearContents
Range("B17").Select
Selection.ClearContents
Columns("C:C").ColumnWidth = 8.29
Columns("C:C").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("E:E").Select
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
Columns("I:I").EntireColumn.AutoFit
Columns("J:J").EntireColumn.AutoFit
Range("Q1").Select
Columns("K:K").EntireColumn.AutoFit
Columns("L:L").EntireColumn.AutoFit
Range("R1").Select
Columns("M:M").EntireColumn.AutoFit
Columns("N:N").EntireColumn.AutoFit
Columns("O:O").EntireColumn.AutoFit
ActiveWindow.SmallScroll Down:=6
ActiveWindow.SmallScroll ToRight:=-9
ActiveWindow.SmallScroll Down:=-88
End Sub

As I just did have the time I reorganized your code a little. Be aware that this is not commonly done here on stackoverflow. For next time: At least try to code something, if it's wrong that's not a problem, that's where we can help. And for your information: I am quite the newby as well (3,5 months of vba so far), so it's not that hard. Even if my code is not perfected yet, most of the time I can get it to work somehow...
Try this once (read the comments in the code first):
Sub UMR()
Dim WS As worksheet
Set WS = AcitveWorkbook.ActiveWorksheet 'be aware this will always be run on the activesheet
Dim Values AS Variant
Values = Array("Transaction_Type", "Meter_Point_Ref", "Actual_Read_Date", "Meter_Reading_Source", "Meter_Reading_Reason", "Meter_Serial_Number", "Meter_Reading", "Meter_ROC_Count", "Meter_Read_Verified", "Corrector_serialNumber", "Corrector_serial_Number", "Corrector_Uncorrected_Reading", "Corrector_Corrected_Reading", "Corrector_ROC_Count", "Corrector_Usable_IND", "Corrector_Read_Verified")
Dim FindString As String
FindString = "ZFD"
Dim ZFDVal As Variant
Dim IRow As Integer
Dim ICol As Integer
Set ZFDVal = Ws.Find(What:=FindString, _
After:=Ws.Cells(Ws.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _ 'If the value is only a part of a cell it would be xlPart instead of xlWhole
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False) 'If you want it to Match the string exactly (regarding capital letters) you'll have to set this to true
IRow = Range(ZFDVal.Adress).Row 'This is untested...
For ICol = 1 To (UBound(Values)-LBound(Values))
Ws.Cells(IRow, ICol) = Values(ICol-1)
Next ICol
Range("A17").Clear ' I believe this was unintendet and just recorded alongside so you can delete these two rows...
Range("B17").Clear
Columns("A:O").EntireColumn.AutoFit
End Sub
If you get a run-time error please press "debug" and comment which line gets marked yellow. This way we can help you correcting the code...

Related

MS Excel - VBA - Issues with searching a #, then returning relevant cells to different worksheet

I've got a workbook in Excel that I can add orders at my work and it stores into a database. I have another sheet that you can type in an order number (ECO number in the code) and I want it to display the # plus any relevant part numbers. I am having issue getting it to select only the range that I need.
Here is what I have in VBA so far:
Sub PlayMacro()
Dim Prompt As String
Dim RetValue As String
Dim Rng As Range
Dim RowCrnt As Long
Prompt = ""
With Sheets("ECO Database")
Do While True
RetValue = InputBox(Prompt & "Type in ECO#")
If RetValue = "" Then
Exit Do
End If
Set Rng = .Columns("A:A").Find(What:=RetValue, After:=.Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
Prompt = "ECO""" & RetValue & """Not Found"
Else
Sheets("ECO Updates").Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveWindow.SmallScroll Down:=3
ActiveCell.Range("A1:T49").Select
Selection.Delete Shift:=xlToLeft
Sheets("ECO Database").Select
ActiveCell.Offset(-2, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Range("A:U").Select
Selection.Copy
Sheets("ECO Updates").Select
ActiveCell.Select
ActiveSheet.Paste
End If
Prompt = Prompt & vbLf
Loop
End With
End Sub
I have gotten it to pop up with a dialog box and ask for the number I want. I type it in, and I get errors due to my programming mistakes, or it selects everything in the sheet (All cells) and memory issues arise. I used the macro recorder so you can see that I'm very much a novice.
I have in column A the ECO number that I'm searching for. In Columns B through U I have the data I want. When I add the ECO orders into the database, I've left one blank row between all of them. I thought it would be easier to find where they end, but obviously I'm having difficulty. The reason I want to copy/eventually cut the data is so that any ECO can be adjusted and then "Saved" (Copy/Cut back to the database). Any suggestions would be hugely appreciated!
PS I'm not allowed to add images yet it says, otherwise I would show you what the format is.
Here is what I got as a solution after more time working on this! I have updated my entire sheet with some extras also. This achieves the result I want! Now I'm just struggling getting it to display a msg box with an error rather than a compiling error when I search for something that isn't found. You'll have to ignore the comments behind the ' as that is me trying to get this damn thing to say Not Found. It is coming up with that message when I search for something that DOES exist. A different problem for a different day.
Sub ECO_SEARCH()
Dim val As String
'Dim Rng As Range
'Is B1 empty?
If IsEmpty(Range("B1").value) = True Then
MsgBox "Please enter an ECO#"
Range("A3:T150").Select
Selection.ClearContents
GoTo LastLine
End If
Sheets("ECO Updates").Select
Range("A3:T150").Select
Selection.Delete Shift:=xlToLeft
ActiveWindow.SmallScroll Down:=-21
Range("B1").Select
val = Range("B1").value
'ActiveCell.FormulaR1C1 = "B1"""
Sheets("ECO Database").Select
Range("A3").Select
Cells.Find(What:=val, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase _
:=False, SearchFormat:=False).Activate
'If Not Rng Is Nothing Then
' MsgBox "ECO# Not Found!"
' GoTo LastLine
'End If
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight).Offset(0, 18)).Select
Selection.Copy
Sheets("ECO Updates").Select
Range("A3").Select
ActiveSheet.Paste
Range("A3:T150").HorizontalAlignment = xlCenter
Range("A3:T150").Font.Size = 10
Range("A3:T150").Font.Bold = False
Range("A3:T150").Borders(xlEdgeBottom).Weight = xlThin
Range("A3:T150").Borders(xlEdgeTop).Weight = xlThin
Range("A3:T150").Borders(xlEdgeLeft).Weight = xlThin
Range("A3:T150").Borders(xlEdgeRight).Weight = xlThin
Range("A3:T150").Borders(xlInsideHorizontal).Weight = xlThin
Range("A3:T150").Borders(xlInsideVertical).Weight = xlThin
Range("A3:T150").Borders(xlEdgeBottom).LineStyle = xlContinuous
Range("A3:T150").Borders(xlEdgeTop).LineStyle = xlContinuous
Range("A3:T150").Borders(xlEdgeLeft).LineStyle = xlContinuous
Range("A3:T150").Borders(xlEdgeRight).LineStyle = xlContinuous
Range("A3:T150").Borders(xlInsideHorizontal).LineStyle = xlContinous
Range("A3:T150").Borders(xlInsideVertical).LineStyle = xlContinous
Range("A3:T150").Borders(xlEdgeBottom).Color = vbBlack
Range("A3:T150").Borders(xlEdgeTop).Color = vbBlack
Range("A3:T150").Borders(xlEdgeLeft).Color = vbBlack
Range("A3:T150").Borders(xlEdgeRight).Color = vbBlack
Range("A3:T150").Borders(xlInsideHorizontal).Color = vbBlack
Range("A3:T150").Borders(xlInsideVertical).Color = vbBlack
Range("A3:T150").HorizontalAlignment = xlCenter
Range("A3:T150").VerticalAlignment = xlCenter
LastLine:
End Sub

I need help truncating characters beyond 40 in the one column in VBA

My code fails to truncate anything beyond 40 characters when i run it. Any suggestions on what line I can use. The code is an xlam. I am trying to truncate anything in column G I tried to put in formula left(F2, 40). Maybe i am using a wrong formula? or there is another way to fix it. Please let me know. Here is the Code I have so far:
Option Explicit
Private Sub ProcessReport()
Dim oWB As Excel.Workbook
Dim oXLAM As Excel.Workbook
Dim oWS As Excel.Worksheet
Set oWB = ActiveWorkbook
Set oWS = ActiveSheet
Set oXLAM = Workbooks("NRPPosPay.xlam")
Call formatcols
End Sub
Sub formatcols()
Dim oWS As Excel.Worksheet
Dim LastPopulatedRow As Long
LastPopulatedRow = Range("G" & Rows.Count).End(xlUp).Row
'Delete Colums
Columns("F:H").Select
Selection.Delete Shift:=xlToLeft
'Move Colums
Columns("E:E").Select
Selection.Cut
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Columns("E:E").Select
Selection.Cut
Columns("D:D").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("E:E").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("F:F").Select
Selection.Insert Shift:=xlToRight
'Replace in Colums
Range("G:G").Replace What:=",", Replacement:=" "
Range("C:C").Replace What:="-", Replacement:=" "
Range("E:E").Replace What:="OCK", Replacement:="IS"
Range("E:E").Replace What:="VCK", Replacement:="CN"
'Formula in Columns
Range("G2").Formula = "=left(G2, 40)"
Range("G2: " & "G" & LastPopulatedRow).FillDown
'Copy and Paste
Range("G:G").Copy
Range("F:F").PasteSpecial _
Operation:=xlPasteSpecialOperationDivide
'Delete Column
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
'Replace
Range("F:F").Replace What:=",", Replacement:=" "
'Format columns
Columns("A:A").NumberFormat = "0"
Columns("B:B").NumberFormat = "0"
Columns("C:C").NumberFormat = "#.00"
Columns("D:D").NumberFormat = "mddyyyy"
' Delete Header row
Rows(1).EntireRow.Delete
End Sub
Sub ProcessPos(control As IRibbonControl)
Call ProcessReport
End Sub
Leaving all other changes that you can do to improve the code you should replace this line
Range("G2").Formula = "=left(G2, 40)"
with this one:
Range("G2").Value = Left(Range("G2").Value, 40)
This would make your code do what you desire for cell G2 now you can use loop to do this for all cells.

Setting the default value based on the adjacent cell in VBA

Sub Print_New()
'
' Print_New Macro
'
'
ActiveSheet.Unprotect
ActiveSheet.Range("$B$7:$G$24").AutoFilter Field:=1, Criteria1:="<>"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
ActiveSheet.Range("$B$7:$G$24").AutoFilter Field:=1
ActiveSheet.Protect
Sheets("Bill (1)").Copy Before:=Sheets(5)
ActiveSheet.Unprotect
Range("C8:C17,D20,E20:F20").Select
Range("E20").Activate
Selection.ClearContents
Range("G20").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
Range("F8").Select
Range("F8").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F9").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F10").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F11").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F12").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F13").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F14").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F15").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F16").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F17").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("C8").Select
ActiveSheet.Protect
ActiveWorkbook.Save
End Sub
Need a proper code instead of any "IF" formula.
When I write something in any cell in the range C8:C17, the default value 1 should be equal to the same cell in the range F8:F17. Which can be changed. And when C8:C17 is empty then F8:F17 should also be empty.
Please don't do the constant Select and ActiveCell: you might replace:
Range("G20").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
by:
Range("G20").FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
And, instead of using RC, you might do the following:
Range("G20").Formula = "=IF(Offset(-2;0)="""","""",5%)"
In top of this, you can use the whole range of F8:F17:
Range("F8:F17").Formula = "IF(Offset(-3;0)>0,1,"""")"
This is already a big decrease of obsolete code.

How to get my VBA Code to loop through all sheets in workbook?

I have 5 worksheets in my workbook(Table 1, Table 2, Table 3, Table 4 and Combined). The main Worksheet is the one I am trying to combine the other 4 into and place the data on the next blank line.
I have been googling different code solutions for weeks to no avail.
When I step through the macro and use the loop (do while, for and Each), it is only looping through Table 1 perfectly. But I cannot get it to loop through Sheets 2-4.
I think I know where my issue is, but in my weeks of googling, I still can't find the solution. I think the issue is on the line where it reads "Sheets("Table 1").Select". Because the code seems to work till it gets to that line. Then it "of course" goes back to Table 1.
This is a test group for a much larger project. I have to pull information from 500 documents that are all set up in the exact same position, but I have to get these 4 to work first.
'I have tried this:
Dim iSheet As Object
For Each iSheet In ThisWorkbook.Sheets
MsgBox iSheet.Name
Next iSheet
'And I tried this:
Dim useWorkSheet As Worksheet
Dim totalWorkSheet As Worksheet
Dim tableAsNumeric As Integer
Dim startingTable As Integer
'For Each Current In Worksheets
'Table Name = Table in Worksheets
startingTable = 1
Set totalWorkSheet = ActiveWorkbook.Sheets("Table 1")
For Each useWorkSheet In ActiveWorkbook.Worksheets
tableAsNumeric = Val(useWorkSheet.Name)
'If tableAsNumeric >= startingTable Then
'Do While I >= Worksheet("Table 1")
'I = I + 1
'I have also tried a for loop and as many others as there are out on the net... Nothing works...
This is the code I need help with:
Sub TFRdataExtract()
'
' TFRdataExtract Macro
' Extract Data from Individual TFR files to the combined file.
'
' Keyboard Shortcut: Ctrl+e
'
Dim iSheet As Object
For Each iSheet In ThisWorkbook.Sheets
MsgBox iSheet.Name
Sheets("Table 1").Select
Range("AB1").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-27], 7,100)"
Range("AC1").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-24], 14,100)"
Range("AD1").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-19],23,100)"
Range("AE1").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-10],22,100)"
Range("AF1").Select
ActiveCell.FormulaR1C1 = "=MID(R[1]C[-31], 23,100)"
Range("AG1").Select
ActiveCell.FormulaR1C1 = "=MID(R[1]C[-16], 10,100)"
Range("AH1").Select
ActiveCell.FormulaR1C1 = "=MID(R[1]C[-13],13,100)"
Range("AI1").Select
ActiveCell.FormulaR1C1 = "=MID(R[2]C[-34],22,100)"
Range("AJ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[2]C[-25],18,100)"
Range("AK1").Select
ActiveCell.FormulaR1C1 = "=MID(R[2]C[-16],21,100)"
Range("AL1").Select
ActiveCell.FormulaR1C1 = "=MID(R[3]C[-37],21,100)"
Range("AM1").Select
ActiveCell.FormulaR1C1 = "=MID(R[3]C[-28],17, 100)"
Range("AN1").Select
ActiveCell.FormulaR1C1 = "=MID(R[3]C[-21],34,100)"
Range("AO1").Select
ActiveCell.FormulaR1C1 = "=MID(R[4]C[-40],28,100)"
Range("AP1").Select
ActiveCell.FormulaR1C1 = "=MID(R[4]C[-35], 7,100)"
Range("AQ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[4]C[-34],10,100)"
Range("AR1").Select
ActiveCell.FormulaR1C1 = "=MID(R[4]C[-29],10,100)"
Range("AS1").Select
ActiveCell.FormulaR1C1 = "=MID(R[4]C[-21],22,100)"
Range("AT1").Select
ActiveCell.FormulaR1C1 = "=MID(R[5]C[-45],26,100)"
Range("AU1").Select
ActiveCell.FormulaR1C1 = "=MID(R[6]C[-46],18,100)"
Range("AV1").Select
ActiveCell.FormulaR1C1 = "=MID(R[6]C[-37],55,100)"
Range("AW1").Select
ActiveCell.FormulaR1C1 = "=MID(R[7]C[-48],36,100)"
Range("AX1").Select
ActiveCell.FormulaR1C1 = "=MID(R[7]C[-39],30,100)"
Range("AY1").Select
ActiveCell.FormulaR1C1 = "=MID(R[7]C[-28],12,100)"
Range("AZ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[8]C[-51],20,100)"
Range("BA1").Select
ActiveCell.FormulaR1C1 = "=MID(R[8]C[-35],12,100)"
Range("BB1").Select
ActiveCell.FormulaR1C1 = "=MID(R[8]C[-31],20,100)"
Range("BC1").Select
ActiveCell.FormulaR1C1 = "=MID(R[9]C[-54],25,100)"
Range("BD1").Select
ActiveCell.FormulaR1C1 = "=MID(R[9]C[-45],15,100)"
Range("BE1").Select
ActiveCell.FormulaR1C1 = "=MID(R[9]C[-39],23,100)"
Range("BF1").Select
ActiveCell.FormulaR1C1 = "=MID(R[10]C[-57],17,100)"
Range("BG1").Select
ActiveCell.FormulaR1C1 = "=MID(R[10]C[-56],17,100)"
Range("BH1").Select
ActiveCell.FormulaR1C1 = "=MID(R[10]C[-52],13,100)"
Range("BI1").Select
ActiveCell.FormulaR1C1 = "=MID(R[10]C[-42],14,100)"
Range("BJ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[10]C[-38],15,100)"
Range("BK1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-62],11,100)"
Range("BL1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-62],12,100)"
Range("BM1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-59],10,100)"
Range("BN1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-57], 7,100)"
Range("BO1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-55],7,100)"
Range("BP1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-55],11,100)"
Range("BQ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-53],12,100)"
Range("BR1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-50],8,100)"
Range("BS1").Select
ActiveCell.FormulaR1C1 = "=MID(R[12]C[-47],12,100)"
Range("BT1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-71],10,100)"
Range("BU1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-71],20,100)"
Range("BV1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-66],10,100)"
Range("BW1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-63],10,100)"
Range("BX1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-62],8,100)"
Range("BY1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-61],7,100)"
Range("BZ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-59],9,100)"
Range("CA1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-57],10,100)"
Range("CB1").Select
ActiveCell.FormulaR1C1 = "=MID(R[13]C[-55],13,100)"
Range("CC1").Select
ActiveCell.FormulaR1C1 = "=MID(R[14]C[-80],12,100)"
Range("CD1").Select
ActiveCell.FormulaR1C1 = "=MID(R[14]C[-80],13,100)"
Range("CE1").Select
ActiveCell.FormulaR1C1 = ""
Range("CE1").Select
ActiveCell.FormulaR1C1 = "=MID(R[14]C[-77],15,100)"
Range("CF1").Select
ActiveCell.FormulaR1C1 = "=MID(R[14]C[-72],7,100)"
Range("CG1").Select
ActiveCell.FormulaR1C1 = "=MID(R[14]C[-71],13,100)"
Range("CH1").Select
ActiveCell.FormulaR1C1 = "=MID(R[14]C[-67],14,100)"
Range("CI1").Select
ActiveCell.FormulaR1C1 = "=MID(R[14]C[-62],7,100)"
Range("CJ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[15]C[-87],13,100)"
Range("CK1").Select
ActiveCell.FormulaR1C1 = "=MID(R[15]C[-85],15,100)"
Range("CL1").Select
ActiveCell.FormulaR1C1 = "=MID(R[15]C[-82],11,100)"
Range("CM1").Select
ActiveCell.FormulaR1C1 = "L16,11,100)"
Range("CN1").Select
ActiveCell.FormulaR1C1 = "=MID(R[15]C[-73],15,100)"
Range("CO1").Select
ActiveCell.FormulaR1C1 = "=MID(R[15]C[-68],8,100)"
Range("CP1").Select
ActiveCell.FormulaR1C1 = "=MID(R[17]C[-93],19,100)"
Range("CQ1").Select
ActiveCell.FormulaR1C1 = "=MID(R[17]C[-80],22,100)"
Range("CR1").Select
ActiveCell.FormulaR1C1 = "=MID(R[18]C[-95],27,100)"
Range("CS1").Select
ActiveCell.FormulaR1C1 = "=MID(R[18]C[-82],18,100)"
Range("CT1").Select
ActiveCell.FormulaR1C1 = "=MID(R[19]C[-97],45,100)"
Range("CU1").Select
ActiveCell.FormulaR1C1 = "=MID(R[19]C[-89],22,100)"
Range("CV1").Select
ActiveCell.FormulaR1C1 = "=MID(R[19]C[-81],49,100)"
Range("CW1").Select
ActiveCell.FormulaR1C1 = "=MID(R[20]C[-91],21,100)"
Range("CX1").Select
ActiveCell.FormulaR1C1 = "=MID(R[21]C[-101],16,100)"
Range("CY1").Select
ActiveCell.FormulaR1C1 = "=MID(22,27,100)"
Range("CZ1").Select
ActiveWindow.SmallScroll Down:=-3
Range("CY1").Select
ActiveWindow.SmallScroll ToRight:=-50
Range("AB1:CY1").Select
Range("CY1").Activate
Selection.Copy
Sheets("Combined").Select
Rows("2:2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next iSheet
End Sub
I need to loop through all 4 Worksheets and paste data onto the Combined file into the next blank line.
try this:
For sht = 1 To Sheets.Count
Debug.Print sht
'your code here
Sheets(sht).Activate'or
Sheets(Sheets(sht).Name).Activate
Next
Perhaps this will help. Commented to help understand what is going on.
'// Modify as desired, like to empty rows/columns.
Private Function GetRangeToCopy(zWorksheet As Worksheet) As Range
Set GetRangeToCopy= zWorksheet.UsedRange
End Function
'// Modify to add spacing or whatnot.
Private Function GetDestinationRange(zDestinationWorksheet As Worksheet, zRowCount As Long, zColumnCount As Long) As Range
Dim zReturnRange As Range
Dim zNewRowIndex As Long
Let zNewRowIndex = zDestinationWorksheet.UsedRange.End.Row + 3 '// Increase to add more rows between inserts.
Set zReturnRange = zDestinationWorksheet.
Set GetDestinationRange = zReturnRange
End Function
'// Copies a range to the destination range.
Private Sub CopyRange(zSourceRange As Range, zDestinationRange As Range)
'// This is where copying styles and such would be done.
'// We will just call copy for simplicity.
'// Clear.
Call zDestinationRange.Clear
'// Copy.
Call zSourceRange.Copy(zDestinationRange)
End Sub
'// Copy worksheets to a destination worksheet.
'// Destination worksheet can be a worksheet loaded into a different workbook altogether.
Public Sub CopyWorksheetsTo(zDestinationWorksheet As Worksheet, zClearDestinationWorksheet As Boolean = False _
zPopupCurrentWorksheet As Boolean = True)
Dim zCurrentWorksheet As Worksheet
Dim zCurrentWorksheet_Var As Variant
Dim zRangeToCopy As Range
Dim zDestinationRange As Range
'// Clear destination.
If (zClearDestinationWorksheet) Then
Call zDestinationWorksheet.UsedRange.Clear
End If
'// Cycle through each worksheet in the workbook.
ForEach zCurrentWorksheet_Var in Worksheets
'// this allow us the Intellisense while coding.
Set zCurrentWorksheet = zCurrentWorksheet_Var
'// Make sure this isn't the destination worksheet.
If (zCurrentWorksheet.Name <> zDestinationWorksheet.Name) Then
'// Popup worksheet name.
If (zPopupCurrentWorksheet) Then
Call MsgBox(zCurrentWorksheet.Name)
End If
'// Get range to be copied.
Set zRangeToCopy = GetRangeToCopy(zCurrentWorksheet)
'// Get destination range.
Set zDestinationRange = GetDestinationRange(zDestinationWorksheet)
'// Copy range.
Call CopyRange(zRangeToCopy, zDestinationRange)
End If
Next xCurrentWorksheet_Var
End Sub
To loop on all Worksheets
Example
Option Explicit
Public Sub Example()
' // Declare your Variables
Dim Sht As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
'// loop on all sheets
For Each Sht In Worksheets
Debug.Print Sht.Name
'Do something
Next
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub
Try this code snippet. I am already using this in a macro.
Sub Combine()
' ensure you have placed the "combined" worksheet as the first sheet
'variable declaration
Dim J As Integer
'copying header row from second sheet
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=ThisWorkbook.Sheets("combined").Range("A1")
'copying data from other sheets
For J = 2 To 4
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=ThisWorkbook.Sheets("combined").Range("A65536")_
.End(xlUp) (2)
Next
ThisWorkbook.Worksheets("combined").Columns.AutoFit
End Sub
In my opinion you should try to avoid .Select. Try:
Option Explicit
Sub test()
Dim ws As Worksheet
With ThisWorkbook
For Each ws In .Worksheets
If ws.Name = "Table 1" Then
With ws
.Range("AB1").FormulaR1C1 = "=MID(RC[-27], 7,100)"
.Range("AC1").FormulaR1C1 = "=MID(RC[-24], 14,100)"
.Range("AD1").FormulaR1C1 = "=MID(RC[-19],23,100)"
.Range("AE1").FormulaR1C1 = "=MID(RC[-10],22,100)"
.Range("AF1").FormulaR1C1 = "=MID(R[1]C[-31], 23,100)"
'....... Add more formulas
.Range("AB1:CY1").Copy
End With
With .Worksheets("Combined").Range("A2")
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
End If
Next ws
End With
End Sub
This should work:
Sub TFRdataExtract()
Dim iSheet As Worksheet, rngCopy As Range
For Each iSheet In ThisWorkbook.WorkSheets
If iSheet.Name Like "Table*" Then
With iSheet '<< no need to activate!
.Range("AB1").FormulaR1C1 = "=MID(RC[-27], 7,100)" '<< no need to select!
.Range("AC1").FormulaR1C1 = "=MID(RC[-24], 14,100)"
.Range("AD1").FormulaR1C1 = "=MID(RC[-19],23,100)"
'etc etc
Set rngCopy = .Range("AB1:CY1")
End with
'assign values directly
With ThisWorkbook.Sheets("Combined").Range("A2")
.Resize(rngCopy.Rows.Count, _
rngCopy.Columns.Count).Value = rngCopy.Value
End with
End If 'EDIT - added
Next iSheet
End Sub

Macros to copy and paste in loop

For many of you this might be simple. I need to copy and paste data from 2 specific columns to a new spread sheet; but the macros need to stop when it reaches the end of data or when a cell is blank.
So far the code looks like this:
Workbooks.Add
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!R1C1"
Range("A1").Select
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!RC1"
Range("B1").Select
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!R1C3"
Range("B1").Select
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!RC3"
Range("A1:B1").Select
Selection.Copy
Range("A2:A101").Select
ActiveSheet.Paste
Range("A1").Select
Windows("Muhanad_Reset_import-TEMPLATE.xlsx").Activate
I know the code for copy and paste code does not work for a good result.
Do you mean code like this:
Sub Test()
Dim lr As Long
Dim i As Long
lr = ThisWorkbook.Worksheets("Reset_import-Raw Data").Cells(Rows.Count, 1).End(xlUp).Row
Workbooks.Add
With ActiveSheet.Range("A1:A" & lr)
.Formula = "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!$A1"
.Offset(, 1).Formula = "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!$C1"
End With
Windows("Muhanad_Reset_import-TEMPLATE.xlsm").Activate
End Sub

Resources