NEED TO CORRECT THIS CODE

wndncg

Board Regular
Joined
Mar 24, 2017
Messages
84
Office Version
  1. 2019
  2. 2016
  3. 2013
Basically it adds the data where it based on cell A1 & E1

What i want:

currently the code adds the data on the end of the cell, what i want to add on the current cell:

---

Sub DATA_DATE_LAST()

Dim wsData As Worksheet
Dim wsTemp As Worksheet
Dim v As Variant
Dim LR As Long
Dim r As Long

Application.ScreenUpdating = False

' Set worksheet variables
Set wsData = Sheets("TEMPLATE")
Set wsTemp = Sheets("FIN")

' Capture value to filter on
v = wsData.Range("A1")
dd = wsData.Range("E1")

' First clear range on TEMPLATE_SHEET
' wsTemp.Activate
' Rows("4:" & Rows.Count).Delete

' Find last row on DATA_SHEET
wsData.Activate
LR = Cells(Rows.Count, "B").End(xlUp).Row

' Loop through all rows on DATA_SHEET
For r = 1 To LR
' Check value in column A
If Cells(r, "B") = v And Cells(r, "A") = dd Then
' Copy columns B-D to TEMPLATE_SHEET
Range(Cells(r, "A"), Cells(r, "D")).Copy wsTemp.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

Range(Cells(r, "E"), Cells(r, "K")).Copy wsTemp.Cells(Rows.Count, "N").End(xlUp).Offset(1, 0)

Range(Cells(r, "L"), Cells(r, "M")).Copy wsTemp.Cells(Rows.Count, "X").End(xlUp).Offset(1, 0)
End If
Next r
'Call ADD_SEQ_01
Application.Wait (Now + TimeValue("00:00:01"))
'Call ADD_SEQ_02
Application.ScreenUpdating = True
wsTemp.Activate
MsgBox "ADDED_-WNDNCG"
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
the code is working but i need to edit this: is this possible?

rowOutTemp = wsTemp(ActiveCell.Row)
 
Upvote 0
No that syntax won't work, you will need something like
VBA Code:
wsTemp.Activate
rowOutTemp = Activecell.Row
can you also with this? :3

 
Upvote 0
Thanks for letting us know. Glad we could help.

PS: I will have a look, might need to be tomorrow though.
 
Upvote 0
hello sir tell me if this is not on the main subject now, so currently i added a switch (OLD/NEW)
1663747893550.png


What i want to incorporate this to this code below, will add the data based on "NEW" value link of the final file below
---
Sub DATA_DATE_MANUAL_SWITCH()
Dim wsData As Worksheet
Dim wsTemp As Worksheet
Dim v As Variant
Dim LR As Long
Dim r As Long
Dim dd As Date
Dim rowOutTemp As Long

Application.ScreenUpdating = False
' Set worksheet variables
Set wsData = Sheets("TEMPLATE")
Set wsTemp = Sheets("FIN")
v = wsData.Range("A1")
dd = wsData.Range("C1")
wsTemp.Activate
rowOutTemp = ActiveCell.Row ' XXX request was to output to Current Cell
' Capture value to filter on
Application.Wait (Now + TimeValue("00:00:01"))
Rows(ActiveCell.Row).Select
' First clear range on TEMPLATE_SHEET
' wsTemp.Activate
' Rows("4:" & Rows.Count).Delete
' Find last row on DATA_SHEET
' wsData.Activate ' XXX remove activation of sheet
LR = wsData.Cells(Rows.Count, "B").End(xlUp).Row

' Loop through all rows on DATA_SHEET
For r = 1 To LR
With wsData
' Check value in column A
If .Cells(r, "B") = v And .Cells(r, "A") = dd Then
' Copy columns B-D to TEMPLATE_SHEET
.Range(.Cells(r, "A"), .Cells(r, "D")).Copy wsTemp.Cells(rowOutTemp, "A")
.Range(.Cells(r, "E"), .Cells(r, "K")).Copy wsTemp.Cells(rowOutTemp, "N")
.Range(.Cells(r, "L"), .Cells(r, "M")).Copy wsTemp.Cells(rowOutTemp, "X")
rowOutTemp = rowOutTemp + 1
End If
End With
Next r

Application.ScreenUpdating = True
MsgBox "ADDED_-WNDNCG"
End Sub
 
Upvote 0
I have no idea what the relationship is between code and the switch or what the switch is actually meant to do.
Nor does it tell me what sheet and cell the value that is going to be used is contained in.
 
Upvote 0
I have no idea what the relationship is between code and the switch or what the switch is actually meant to do.
Nor does it tell me what sheet and cell the value that is going to be used is contained in.
this one if maybe there is a condition where what value on column N if it is OLD or NEW

If .Cells(r, "B") = v And .Cells(r, "A") = dd Then
 
Upvote 0
If it doesn't impact on the other actions in the If statement you will need to have another If statement inside the previous one such as:

VBA Code:
If .Cells(r, "B") = v And .Cells(r, "A") = dd Then
    ' Copy columns B-D to TEMPLATE_SHEET
    .Range(.Cells(r, "A"), .Cells(r, "D")).Copy wsTemp.Cells(rowOutTemp, "A")
    .Range(.Cells(r, "E"), .Cells(r, "K")).Copy wsTemp.Cells(rowOutTemp, "N")
    .Range(.Cells(r, "L"), .Cells(r, "M")).Copy wsTemp.Cells(rowOutTemp, "X")
    
    If UCase(.Cells(r, "N")) = "OLD" Then
        ' do old action
    ElseIf UCase(.Cells(r, "N")) = "NEW" Then
        ' do NEW action
    Else
        ' what do do if neither are true
        ' this could be to do nothing
    End If
    
    rowOutTemp = rowOutTemp + 1
End If
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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