Copy columns from sheet Sales to SavedData

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy columns from sheet Sales to SavedData

Post by adam »

The second part of the macro (ToPat) adds one row to the PatDetails sheet, but the data appear to be shifted. So you'll have to edit the code so that it fills the correct cells.
I can manage this part of the code
You already have the basic code, it is up to you to modify it. You can't expect us to do that for you.
I hope I never had expectations beyond the limit.
The first part of the macro fills columns F:J of the Database sheet correctly. It doesn't fill columns A:E because you haven't written code to fill columns A:E.
This is the part that I was not getting in my code. That's why I did ask help to find me what I had wrong in the code.

Anyways thanks for the reply.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy columns from sheet Sales to SavedData

Post by HansV »

If you have a general question about how to do something in Excel, you are very welcome to ask it here. I and others will do our best to answer it.
But asking us to modify already existing code each time you change the layout of a worksheet is beyond the scope of a discussion forum. We provide help but we don't develop software for others.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy columns from sheet Sales to SavedData

Post by adam »

If you have a general question about how to do something in Excel, you are very welcome to ask it here. I and others will do our best to answer it.
Hans, what you've said is completely correct. And I do really appreciate the work you have done so far. Moreover I can say that I feel happy with that.
But asking us to modify already existing code each time you change the layout of a worksheet is beyond the scope of a discussion forum. We provide help but we don't develop software for others.
I hope I never did ask help in that manner. And me too never develop softwares for others as I'm a single simple guy. :cheers:

Instead I do Excel datas for my own purpose.

Anyways, keep smiling & enjoy life.

Thanks for the reply.

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy columns from sheet Sales to SavedData

Post by adam »

When using the following code it copies the data as mentioned in the code from the “Orders” sheet to “PatDetails” & “Database”. But it copies extra five rows within the columns A to E to the sheet "Database" each time I run the macro .
What might I have done wrong in coding that is causing the problem.

I would be happy if you could explain me what I have done wrong in here. :scratch:

Code: Select all

Sub SaveData()
On Error Resume Next
Application.ScreenUpdating = False

Dim r As Long
Dim m As Long
Dim t As Long

Dim strCategory As String
Dim wshSource As Worksheet
Dim wshTarget As Worksheet
Set wshSource = Worksheets("Orders")
Set wshTarget = Worksheets("Database")

Dim n As Long
Dim PatDetailsWks As Worksheet
Dim OrdersWks As Worksheet
Dim DatabaseWks As Worksheet

Dim nextRow As Long
Dim oCol As Long

Dim myRng As Range
Dim myCopy As String
Dim myCell As Range
  
' Determine the last used row
' in column F on the Database sheet
      With wshTarget
        t = .Cells(.Rows.Count, 6).End(xlUp).Row
      End With

      With wshSource
        ' Determine last used row in column A
        ' on the Orders sheet
        m = .Cells(.Rows.Count, 1).End(xlUp).Row
        For r = 14 To m
          If .Cells(r, 1) = "" Then
            If .Cells(r, 2) <> "" Then
              strCategory = .Cells(r, 2)
            End If
          ElseIf .Cells(r, 1) <> "Test Name" Then
            t = t + 1
            wshTarget.Cells(t, 6) = strCategory
            .Range(.Cells(r, 1), .Cells(r, 4)).Copy
            wshTarget.Cells(t, 7).PasteSpecial Paste:=xlPasteValues
          End If
        Next r
      End With

  'cells to copy from Orders sheet - some contain formulas
  myCopy = "B9,F9,F10,F11,F12,F13,F14,H9,H10,H11,H12,H13,H14"

  Set OrdersWks = Worksheets("Orders")
  Set PatDetailsWks = Worksheets("PatDetails")
  Set DatabaseWks = Worksheets("Database")
  
    With OrdersWks
    Set myRng = .Range(myCopy)
  
      If Application.CountA(myRng) <> myRng.Cells.Count Then
      MsgBox "Please fill in all the fields!"
      Exit Sub
    End If
  End With

  r = DatabaseWks.Range("A" & DatabaseWks.Rows.Count).End(xlUp).Row + 1
  ' Copy Date Collected
  OrdersWks.Range("H12").Copy Destination:=DatabaseWks.Range("B" & r & ":B" & (r + m - 16))
  ' Copy Accession No
  OrdersWks.Range("H9").Copy Destination:=DatabaseWks.Range("E" & r & ":E" & (r + m - 16))
  ' Copy Customer ID
  OrdersWks.Range("B9").Copy Destination:=DatabaseWks.Range("C" & r & ":C" & (r + m - 16))
  ' Copy CL
  OrdersWks.Range("F9").Copy Destination:=DatabaseWks.Range("D" & r & ":D" & (r + m - 16))
  ' Time Collected
  OrdersWks.Range("H11").Copy Destination:=DatabaseWks.Range("A" & r & ":A" & (r + m - 16))
  
  DatabaseWks.Range("A10:E10").Copy
  DatabaseWks.Range("A" & r & ":E" & (r + m - 16)).PasteSpecial Paste:=xlPasteFormats
  Application.CutCopyMode = False

  With PatDetailsWks
    nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
  End With

  With PatDetailsWks
    With .Cells(nextRow, "A")
      .Value = Now
      .NumberFormat = "hh:mm:ss"
    End With
    oCol = 2
    For Each myCell In myRng.Cells
      PatDetailsWks.Cells(nextRow, oCol).Value = myCell.Value
      oCol = oCol + 1
    Next myCell
  End With

  With OrdersWks.Range("H9")
    .Value = .Value + 1
  End With
ThisWorkbook.Save
Application.ScreenUpdating = True
End Sub
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy columns from sheet Sales to SavedData

Post by HansV »

The reason for the problem is that you skip some rows from the Orders sheet (the ones that are empty or contain headers). You only want to copy up to and including the last filled row; this is row t on the Database sheet.
So replace ALL instances of (r + m - 16) in the code with t:

OrdersWks.Range("H12").Copy Destination:=DatabaseWks.Range("B" & r & ":B" & t)

etcetera.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy columns from sheet Sales to SavedData

Post by adam »

Thanks Hans. Finally you have made my dream come true.

Once again thanks for the help & I do really appreciate it.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy columns from sheet Sales to SavedData

Post by adam »

Following part of the code writes the time in the format as described in the code to the column A of each row.

Code: Select all

With PatDetailsWks
  With .Cells(nextRow, "A")
  .Value = Now
  .NumberFormat = "hh:mm:ss"
  End With
  oCol = 2
  For Each myCell In myRng.Cells
  PatDetailsWks.Cells(nextRow, oCol).Value = myCell.Value
  oCol = oCol + 1
  Next myCell
  End With
I tried modifying the code by replacing the lines with

Code: Select all

.Value = Today
  .ValueFormat = "Short Date"
So that the code copies the date to the column “A” of the specified sheet instead of the time. But it does not seem to work.
I would be happy if you could let me know that what have I done wrong here
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy columns from sheet Sales to SavedData

Post by HansV »

In the first place, TODAY() is an Excel worksheet function; the equivalent VBA function is Date.
In the second place, ValueFormat is not a valid property of a cell or range; it should be NumberFormat, as in the original code.
In the third place, "Short Date" is a VBA date format, not an Excel date format. The one you want is "m/d/yyyy". This will be translated to the short date format for your system.

So the lines become

.Value = Date
.NumberFormat = "m/d/yyyy"

You can probably omit the second line.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy columns from sheet Sales to SavedData

Post by adam »

Thanks for the instruction & for the reply. Now I could get the date into column "A" of my worksheet.

Thanks for the help
Best Regards,
Adam