I need to write a Loop to run the following lines until the third line finds the cell it is selecting to be empty.
I would normally include my code for the loop to be corrected / critiqued but after searching all morning I can't find an example of do while loop that doesn't count to keep moving.
Can someone point me in the right direction with a link or two?
Sheets("Sheet1").Select
Selection.Offset(0, 3).Select
Selection.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection(1, 3)).Select
Selection.Copy
Sheets("Output").Select
Selection.End(xlDown).Select
Selection.Offset(1, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Include Fund name
Sheets("Sheet1").Select
Selection.End(xlUp).Select
Selection.Copy
Sheets("Output").Select
Range("B2").Select
Selection.End(xlDown).Select
Selection.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.Offset(0, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(0, -1).Select
Selection.FillDown
I can't find an example of do while loop that doesn't count to keep moving.
You didn't look very hard, then. All Do or While loops should have a terminator, or an Exit statement, otherwise they will crash the application in an infinite loop.
Try:
Do Until Sheets("Sheet1").Selection.Offset(1, 0).Value = vbNullString
'// YOUR CODE HERE //
Loop
Or:
Do
If Sheets("Sheet1").Selection.Offset(1, 0).Value = vbNullString Then Exit Do
'// YOUR CODE HERE //
Loop
Related
I have 2 sheets. I want to transfer a fixed range (a row of data) from sheet #1 (data sheet) to sheet #2 (info sheet) by clicking on a command button. The transferred data should be kept as records in sheet #2. But when I use the code below it overwrites the existing entry instead of adding a new record (row). How do I add a new row to sheet #2 and not overwrite the existing one?
Sheets("DATA").Select
Range("B85:G85").Select
Selection.copy
Sheets("INFO").Select
Range("B5").Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("DATA").Select
Range("B86:G86").Select
Application.CutCopyMode = False
Selection.copy
Sheets("INFO ").Select
Range("H5").Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Problem with the Code:
No calculation of Last Row
Excessive use of Select
Try this:
With Sheets("INFO")
Sheets("DATA").Range("B85:G85").Copy
.Range("B" & .Range("B5").End(xlDown).row + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("DATA").Range("B86:G86").Copy
.Range("H" & .Range("H5").End(xlDown).row + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
i'm currently working on a code that will copy data from one sheet to another (please see below),it currently works on one sheet, but now I want this code to work on multiple sheets or whatever sheet is active. Can you please assist me in modifying this code to work on multiple sheets.
Sub sbCopyRangeToAnotherSheet()
Sheets("Sheet1").Select
Range("A15:E188").Select
Selection.ClearContents
Range("A15").Select
Sheets("FCI").Select
Range("C51").Select
'Copy the data
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Activate the destination worksheet
Sheets("Sheet1").Activate
'Select the target range
Range("A15").Select
'Paste in the target destination
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
End Sub
May be this is what you are looking for, also you can yourself twick the code little bit to suit your requirements.
Sub sbCopyRangeToAnotherSheet()
temp = ActiveSheet.Index
Sheets(temp).Select
Range("A15:E188").Copy
Worksheets("Sheet1").Activate
k = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Range("A" & k + 1).Select ' kindly change the code to suit your paste location
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
modified your code with a loop to go through all the sheets present in workbook.
let me know if this is what was required.
Sub sbCopyRangeToAnotherSheet()
Sheets("Sheet1").Select
Range("A15:E188").Select
Selection.ClearContents
Range("A15").Select
Sheets("FCI").Select
Range("C51").Select
'Copy the data
For Each Sheet In ActiveWorkbook.Sheets
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Activate the destination worksheet
Sheets("Sheet1").Activate
'Select the target range
Range("A15").Select
'Paste in the target destination
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Next
End Sub
as I understand by your code, you want to be able to copy data from any active sheet to FSI Sheet. if this is the case , I have done changes as in below code.
Sub sbCopyRangeToAnotherSheet()
temp = ActiveSheet.Index
Sheets(temp).Select
Range("A15:E188").Select
Selection.ClearContents
Range("A15").Select
'Sheets("FCI").Select
'ActiveSheet.Range("C51").Select
'Copy the data
'For k = 1 To ActiveWorkbook.Worksheets.Count
Worksheets(temp).Activate
Sheets(temp).Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Activate the destination worksheet
Sheets(temp).Activate
'Select the target range
Range("A15").Select
'Paste in the target destination
Sheets("FCI").Select
Range("A15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
'Next
End Sub
I am trying to copy a range of cells in a single column and transpose them above
as a row.
The problem I am having is to try to increase the Rows(45:45) where each row is being copied, otherwise all of the rows get overwritten.
When I run the macro I get a syntax error on Destination:= Range line
Range("A53:A63").Copy
Range("A52").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Range("A53:A63").Select
Application.CutCopyMode = False
Selection.EntireRow.Delete
Rows("52:52").Cut
Rows("45:45").Select
Destination:=Range("A" & Rows.Count).End(xlDown).Offset(1, 0)
Selection.Insert
Although I do not really understand what you are trying to do but maybe this helps.
Sub test()
Range("A53:A63").Copy
Range("A52").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Range("A53:A63").Select
Application.CutCopyMode = False
Selection.ClearContents
Rows("52:52").Copy
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Why canĀ“t my code below paste values only?
The result is pastes the formula instead of the value.
Can anyone help?
Please check my code as follow :
Sheets("Invoice Print").Activate
Range("F21:F27").Select
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.Copy
Sheets("Outgoing Goods").Select
Cells(Rows.Count, 1).Range("K1").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
Are there any mistakes?
PS: I'm using Excel 2013.
Please advise.
Thank you.
You overwrite the pasted values right in the next line with ActiveSheet.Paste
Also you should not use .Select and Selection..
Sheets("Invoice Print").Range("F21:F27").SpecialCells(xlCellTypeFormulas, 1).Copy
With Sheets("Outgoing Goods")
.Cells(.Rows.Count, 1).Range("K1").End(xlUp).Offset(1, 0).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'This line would overwrite the pasted values with not explecit values only
'ActiveSheet.Paste
Application.CutCopyMode = False
End With
Hope I could help.
I am trying to use macro recorder in Excel to record a macro to fill down a column of cells, however because the fill down each time is a different number of cells it either fills down to short or too long and this seems to be because the macro identifies the cell range and its fixed.
What I need is to auto populate or auto fill down from:
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[1],"" "",RC[2])"
Range("C1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
ActiveCell.Range("A1:A261").Select
Since file is not always 261? How can I place a command to choose/autofill the last column?
Blockquote
Sub WMEHOT_Cleaner()
'
' WMEHOT_Cleaner Macro
'
'
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("L:L").Select
Selection.Cut
Columns("B:B").Select
ActiveSheet.Paste
Columns("C:C").Select
Selection.Cut
Range("O1").Select
ActiveSheet.Paste
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[1],"" "",RC[2])"
Range("C1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
ActiveCell.Range("A1:A261").Select
Range("C1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.End(xlUp).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("D:E").Select
Selection.ClearContents
Range("D1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[2],"" "",RC[6])"
Range("D1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[5],""_"",RC[6])"
Range("D1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
ActiveCell.Range("A1:A261").Select
ActiveCell.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.End(xlUp).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("I:J").Select
Selection.ClearContents
Columns("O:O").Select
Selection.Cut
Columns("E:E").Select
ActiveSheet.Paste
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("L:L").Select
Selection.Cut
Columns("J:J").Select
ActiveSheet.Paste
Range("J1").Select
Selection.Copy
Range("L1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
ActiveCell.FormulaR1C1 = "=""WMEOnline_""&RC[9]"
Range("A1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
ActiveCell.Range("A1:A261").Select
ActiveCell.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.End(xlUp).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveCell.Offset(2, 0).Range("A1").Select
Range("O5").Select
Cells.Select
Range("A1").Activate
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
msgclean = MsgBox("Cleaning and Sorting Complete!!" & vbNewLine & "File Ready for LMS." & vbNewLine & "Please SAVE this file as CSV Format", vbInformation + vbOKOnly, "WME HOT Cleaner Template")
End Sub
Use the following code:
Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=CONCATENATE(RC[1],"" "",RC[2])"
Replace A if your column is not column A
Use
lr = Cells(Rows.Count, "A").End(xlUp).Row
to find the last row from a column or
Set wks = ActiveWorkbook.Worksheets(sheet)
i = wks.Range("A:A").End(xlDown).Row
j = wks.Cells.End(xlToRight).Column
to find the position of the last cell.