I have a user input transaction information (Name, Amount, Date) in cells E33:G33.
User clicks button and the transaction is recorded in the last row +1 used in a Transaction history list starting on E46:G46.
So far my code doesnt work.
i want simply:
-copy range (E33:G33)
-find last row used in column E
-go one row lower
-past the copied range
Cant find a working answer, please help! Thanks.
use this:
Sub test()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
Range("E" & e & ":G" & e).Value = [E33:G33].Value 'past only values
End Sub
or
Sub test2()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
[E33:G33].Copy Range("E" & e & ":G" & e) 'past with cells format
End Sub
or
Sub test3()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
[E33:G33].Copy
Range("E" & e & ":G" & e).PasteSpecial xlPasteAll 'past with cells format
End Sub
or
Sub test4()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
[E33:G33].Copy
Range("E" & e & ":G" & e).PasteSpecial xlPasteValues 'past only values
End Sub
also:
[E33:G33] can be replaced by:
Range("E33:G33")
or
Cells(Cells(33,"E"),Cells(33,"G"))
or
Cells(Cells(33,5),Cells(33,7))
Related
Very new to VBA and macros. Looking to build a macro that will cut the items in G2:H2, paste them in I2:J2, skip to the next even numbered row, cut those items (G4:H4), paste those into I4:J4, and so-on until the end of the spreadsheet. The spreadsheet has roughly 11200 rows to sort through. Any help is GREATLY appreciated!
Sub TotalMove()
'
' TotalMove Macro
a = 2
g = Sheet17.Cells(Rows.Count, "A").End(xlUp).Row
For a = 2 To g Step 2
Range("G2:H2").Select
Selection.Cut
Range("I2:J2").Select
ActiveSheet.Paste
Next a
End Sub
You almost had it, was just a case of using your row number in the loop.
I made the variable names obvious for demo, set values rather than copy-paste, and specified the sheet in ranges.
Sub TotalMove()
With Sheet17
StartRow = 2
EndRow = .Cells(Rows.Count, "G").End(xlUp).Row
For CurrentRow = StartRow To EndRow Step 2
.Range("I" & CurrentRow & ":J" & CurrentRow) = .Range("G" & CurrentRow & ":H" & CurrentRow).Value
.Range("G" & CurrentRow & ":H" & CurrentRow).Clear
Next CurrentRow
End With
End Sub
I'm trying to write a macro to autofill a range dynamically as you can see in the photo :
I've tried this code and it return an error :
Sub Auto()
Dim selection1 As Range
Dim selection2 As Range
h = 1
g = 1
This two loops I used to detect the the first empty cell in column AZ to refer to its range later
Do Until Cells(h + 1, 52).Value = ""
h = h + 1
Loop
Do Until Cells(g + 1, 1).Value = ""
g = g + 1
Loop
Set selection1 = Range("AZ" & h & ":" & "BD" & h)
Set selection2 = ActiveSheet.Range("AZ" & g & ":" & "BD" & g)
This where I tested the above idea and it worked fine and the range selected as it should as shown in the photo
Range("AZ" & h & ":" & "BD" & h).Select
'Autofill
This where I got the error:
'Selection.AutoFill Destination:=Range("AZ" & g & ":" & "BD" & g), Type:=xlFillDefault
End Sub
Any Ideas?
This will autofill the data down.
The method for finding the last row is the same as going to the last row and pressing Ctrl+Up.
Sub Test()
Dim LastRow1 As Long, LastRow2 As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRow1 = .Cells(.Rows.Count, "AY").End(xlUp).Row 'Find last row in column AY.
LastRow2 = .Cells(.Rows.Count, "AZ").End(xlUp).Row 'Find last row in column AZ.
.Range(.Cells(LastRow2, "AZ"), .Cells(LastRow2, "BD")).AutoFill _
Destination:=.Range(.Cells(LastRow2, "AZ"), .Cells(LastRow1, "BD"))
End With
End Sub
My raw data looks something like this;
std1
std1
deviant
std2
std1
std2
std2
deviant
The "deviants" are presented randomly and thus do not occur every nth row...
I wish to select 1 row before and 1 row after each "deviant" row so I can copy it in another spread sheet.
See code below.
We loop through each row in the column (I have assumed your data is in column A) and when the given value is found, we add the following and prior rows to our selection array. When the loop is complete, we select the rows in the array
Public Sub DeviantSelect()
Dim myRange As Range
Set myRange = Nothing
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
If Cells(i, 1) = "deviant" Then
If myRange Is Nothing Then
Set myRange = Union(Range(i - 1 & ":" & i - 1), Range(i + 1 & ":" & i + 1))
Else
Set myRange = Union(myRange, Range(i - 1 & ":" & i - 1), Range(i + 1 & ":" & i + 1))
End If
myRange.Select
End If
Next
End Sub
The below code copies the cells before and after deviant to another sheet.
Sub check()
Sheet1.Activate
Range("A1").Select
LastRow = Sheets("Sheet1").UsedRange.Rows(Sheets("Sheet1").UsedRange.Rows.Count).Row
For i = 1 To LastRow
Sheet1.Activate
If Range("A" & i).Value = "deviant" Then
Range("A" & i - 1).Select
Selection.Copy
Sheet2.Activate
LastRow2 = Sheets("Sheet2").UsedRange.Rows(Sheets("Sheet2").UsedRange.Rows.Count).Row
If LastRow2 = 1 Then
Range("A" & LastRow2).Activate
Else
Range("A" & LastRow2 + 1).Activate
End If
ActiveSheet.Paste
Sheet1.Activate
Range("A" & i + 1).Select
Selection.Copy
Sheet2.Activate
LastRow2 = Sheets("Sheet2").UsedRange.Rows(Sheets("Sheet2").UsedRange.Rows.Count).Row
Range("A" & LastRow2 + 1).Activate
ActiveSheet.Paste
End If
Next
End Sub
I'm having an issue with trying to expand on the code that I have written. The code I have pasted below is currently working as intended. The issue I have is that I am trying to make the "P2" cell variable. Essentially, I am trying to compare the ("K" & i) cell in the loop to all the dates in the range P2 to AA2 on my sheet. Then if the month and year matches, paste the data in the corresponding column that matched. I have tried replacing the column reference P with another integer, but could not get the nested loop to function correctly. Is there a different way to establish a column as a variable? Thank you for your help.
Sub Test()
Dim i As Integer
Sheets("Sheet1").Select
For i = 3 To 6
If Month(Range("K" & i)) = Month(Range("P2")) And Year(Range("K" & i)) = Year(Range("P2")) And Range("J" & i).Value > "0" Then
Range("J" & i).Copy
Range("P" & i).PasteSpecial xlPasteValues
End If
Next i
End Sub
Just loop through the columns 16 through 27 inside your existing loop:
Sub Test()
Dim i As Integer
Sheets("Sheet1").Select
For i = 3 To 6
'loop columns p through aa
For k = 16 to 27
'Instead of Range() we are using Cell() to refer to the column variabl-y... Cells(<rownum>,<columnnum>)
If Month(Range("K" & i)) = Month(Cells(2,k)) And Year(Range("K" & i)) = Year(Cells(2,k)) And Range("J" & i).Value > "0" Then
Range("J" & i).Copy
Cells(i,k).PasteSpecial xlPasteValues
End If
Next k
Next i
End Sub
I have a problem making a little VBA to copy/paste some datas. I looked around and didn't really find any post who talk of my problem.
Here is my problem: I have 3 worksheets who need to be copied on a fourth worksheet. Each worksheet have between 200 and 650 lines. On the three sheets, it's the columns A, I, J, K, L, M,N who need to be copied on the columns A, C, D, H, I, M, N. The copy paste action need to start on the first blank line of the fourth sheet. This is the last constraint who make it a lot more difficult than I expected. I tried two ways and haven't managed to make it works.
Here is the code (one way is in comments form)
Dim Sh as Worksheet
Dim i as Integer
For Each Sh In Sheets(Array("Janvier", "Février", "Mars"))
For i = 4 To 650
Worksheets("Sh").Range("A & i").Copy Destination:=Worksheets("Calculs").Range("A" & Sheets("Calculs").UsedRange.Rows.Count + 1)
Worksheets("Sh").Range("I & i:J & i").Copy Destination:=Worksheets("Calculs").Range("I" & Sheets("Calculs").UsedRange.Rows.Count + 1)
Worksheets("Sh").Range("K & i:L & i").Copy Destination:=Worksheets("Calculs").Range("K" & Sheets("Calculs").UsedRange.Rows.Count + 1)
Worksheets("Sh").Range("M & i:N & i").Copy Destination:=Worksheets("Calculs").Range("M" & Sheets("Calculs").UsedRange.Rows.Count + 1)
'Sheets("Calculs").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("A4:A650").Value
'Sheets("Calculs").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("I4:J650").Value
'Sheets("Calculs").Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("K4:L650").Value
'Sheets("Calculs").Range("M" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("M4:n650").Value
Next i
Next Sh
My error after executing the code not in comments form is "Subscript out of range". Can you propose me a better way to code this.
Thank you for your help, Olivier
Try using the .Cells method instead of .Range. Like so:
Worksheets("Sh").Cells(i, 1) ...
Where the first parameter is your row and the second is your columns (A=1, B=2, ect).
Try this:
Sub Tester()
Dim Sh As Worksheet, ws As Worksheet, rw As Range
Dim i As Integer
Set ws = Worksheets("Calculs")
'get first empty row
Set rw = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
Application.ScreenUpdating = False
For Each Sh In Sheets(Array("Janvier", "Février", "Mars"))
For i = 4 To 650
Sh.Range("A" & i).Copy rw.Cells(1, "A")
Sh.Range("I" & i & ":J" & i).Copy rw.Cells(1, "I")
Sh.Range("K" & i & ":L" & i).Copy rw.Cells(1, "K")
Sh.Range("M" & i & ":N" & i).Copy rw.Cells(1, "M")
Set rw = rw.Offset(1, 0)
Next i
Next Sh
End Sub