Copying/pasting the selected data from a query table to excel sheet

erfo

New Member
Joined
Aug 5, 2023
Messages
22
Office Version
  1. 2016
Platform
  1. Windows
Copying/pasting the selected data from a query table to excel sheet

I have the following macro that copies the data from an excel query table (not pivot table) to an excel sheet. The macro is a primitive one because of my limited knowledge about vba; so it definitely needs revision. I appreciate any help.

The macro have two parts, because the first part does not paste data to the destination unless I add the second part.

OnChange1.xlsm
ABCDEFGHIJK
1KLMytddaily%RYMXytddaily%DSLX ytddaily%
2August 18, 202334511-5.18%0.00%41.360.63%0.00%22.94-3.78%0.00%
3August 21, 202334543-5.10%0.09%41.470.90%0.27%23.850.04%3.97%
4August 22, 202334388-5.52%-0.45%41.500.97%0.07%23.58-1.09%-1.13%
5August 23, 2023
6August 24, 2023
7August 25, 2023
8August 26, 2023
9      
10      
11      
destntn
Cell Formulas
RangeFormula
D9:D11,D2:D4D2=IF(C2=0,"",(C2-36398)/36398)
G9:G11,G2:G4G2=IF(F2=0,"",(F2-41.1)/41.1)
J9:J11,J2:J4J2=IF(I2=0,"",(I2-23.84)/23.84)
K9:K11,H9:H11,E9:E11,K3:K4,H3:H4,E3:E4E3=IF(C3=0,"",(C3-C2)/C2)


OnChange1.xlsm
ABCDEFGH
1Private Sub Worksheet_Change(ByVal Target As Range)
2Dim src As Worksheet, dest As Worksheet
3Dim NextRow As Long
4 Set src = Sheets("source")
5 Set dest = Sheets("destntn")
6 'Application.EnableEvents = False
7If Target.Address = "$B$2" Then
8 NextRow = dest.Cells(Rows.Count, "C").End(xlUp).Row
9If dest.Range("C" & NextRow).Value <> src.Range("B2").Value Then _
10dest.Range("C" & NextRow + 1).Value = Range("B2").Value
11End If
12
13
14If Target.Address = "$B$3" Then
15 NextRow = dest.Cells(Rows.Count, "F").End(xlUp).Row
16If dest.Range("F" & NextRow).Value <> src.Range("B3").Value Then _
17dest.Range("F" & NextRow + 1).Value = Range("B3").Value
18End If
19
20
21If Target.Address = "$B$4" Then
22 NextRow = dest.Cells(Rows.Count, "I").End(xlUp).Row
23If dest.Range("I" & NextRow).Value <> src.Range("B4").Value Then _
24dest.Range("I" & NextRow + 1).Value = Range("B4").Value
25End If
26
27
28'SECOND PART
29
30 If Target.Address = "$B$2" Then
31 NextRow = dest.Cells(Rows.Count, "C").End(xlUp).Row
32If dest.Range("C" & NextRow).Value <> src.Range("B2").Value Then _
33dest.Range("C" & NextRow + 1).Value = Range("B2").Value
34End If
35
36If Target.Address = "$B$3" Then
37 NextRow = dest.Cells(Rows.Count, "F").End(xlUp).Row
38If dest.Range("F" & NextRow).Value <> src.Range("B3").Value Then _
39dest.Range("F" & NextRow + 1).Value = Range("B3").Value
40End If
41
42If Target.Address = "$B$4" Then
43 NextRow = dest.Cells(Rows.Count, "I").End(xlUp).Row
44If dest.Range("I" & NextRow).Value <> src.Range("B4").Value Then _
45dest.Range("I" & NextRow + 1).Value = Range("B4").Value
46End If
47
48Application.EnableEvents = True
49End Sub
Sheet2
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Here is the source sheet:
OnChange1.xlsm
ABCD
1Namedecimal value
2KLM34388.00
3RYMX41.50
4DSLX 23.58
5
6P.S.: The table above is excel query table (not a pivot table)
7 KLM, RYM and DSLX values are daily refreshed/updated
8
source
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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