0

I am trying to create a VBA module, that takes data from a table in one worksheet and copies it to a second worksheet. This second worksheet should then be exported as a PDF.

The exporting part and naming the PDF is not an issue and I will only tackle this when the copying of the data from one sheet to the other works.

The structure of the table is that I have several rows that have data relevant to the invoice I want to fill on the second sheet and I would like that the macro loops through the whole file and only takes what it needs, but for now I am working on an easier version where I simply want to copy the data from a selection.

Option Explicit
Sub InvoiceCreator()

'create sheet
'Add info to sheet'
'save invoice sheet as PDF with name of customer
'reset sheet
'insert i+1 dataset
'loop til end

Dim sWS As Worksheet
Dim dWS As Worksheet

Dim sRange As Range

Dim sBNR As Range
Dim dBNR As Range

Dim sKNR As Range
Dim dKNR As Range

Dim sREF As Range
Dim dREF As Range

Dim sPRT As Range
Dim dPRT As Range

Dim sDAT As Range
Dim dDAT As Range

Dim sADR As Range
Dim dADR As Range


Dim sDES As String
Dim dDES As String
'Dim sPRC As Range
'Dim dPRC As Range

Dim i As Integer
Dim lastrow As Long

Set sWS = Sheets("Data")
Set dWS = Sheets("Sheet1")

Set sRange = Selection

Set sBNR = sRange.Cells(2, 7)
Set dBNR = dWS.Range("E4")

    dBNR = sBNR.Value

Set sKNR = sRange.Cells(2, 2)
Set dKNR = dWS.Range("E6")

    dKNR = sKNR.Value

Set sREF = sRange.Cells(2, 22)
Set dREF = dWS.Range("E8")

    dREF = sREF.Value

Set sPRT = sRange.Cells(2, 23)
Set dPRT = dWS.Range("E10")

    dPRT = sPRT.Value

Set sDAT = sRange.Cells(2, 4)
Set dDAT = dWS.Range("F4")

    dDAT = sDAT.Value




lastrow = sRange.End(xlUp).Row

For i = 2 To lastrow

sDES = sRange.Cells(i, 12)

dDES = dWS.Range("A" & i + 23)


    dDES = sDES

Next i


End Sub

Most of the code works and copies values from one sheet to the other, but I am stuck with the last loop bit.

I want to take the value of the string in a cell and copy it to a cell in the other sheet and then copy the cell value of the cell below and copy it to the other worksheet one cell below until the end of my selection. I am not getting any error, but it is not copying the data.

Any advice?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
mrmadoff
  • 21
  • 2
  • `Set dBNR = dWS.Range("E4") dBNR = sBNR.Value` Does that work for you properly? I mean, you create a range variable, you assign it to a range, and then, inmediately, you store a value in the variable? And you do this all the time. It makes no sense. – Foxfire And Burns And Burns Apr 01 '19 at 10:59
  • You don't need VBA for this. Excel will do this for you. Have a look at [this](https://stackoverflow.com/questions/49703483/how-can-i-create-a-template-that-will-auto-populate-calculate-cells-based-on-a-s) – Zac Apr 01 '19 at 11:02

0 Answers0