Add data from one table to sheet as value

Alex Bruski

New Member
Joined
Jul 8, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Have a problem with my Excel table. I have a smart table on sheet "Order calculation" and I would like to move this data to sheet "Sales History"

File here

code below:

VBA Code:
Option Explicit

Sub UpdateLogWorksheet()

    Dim historyWks As Worksheet
    Dim inputWks As Worksheet

    Dim nextRow As Long
    Dim oCol As Long

    Dim myRng As Range
    Dim myCopy As String
    Dim myCell As Range
    
    'cells to copy from Input sheet - some contain formulas
     myCopy = ActiveSheet.ListObjects("Table1").DataBodyRange.Select

    Set inputWks = Worksheets("Order calculation")
    Set historyWks = Worksheets("Sales History")

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

   

    With historyWks
        With .Cells(nextRow, "A")
            .Value = Now
            .NumberFormat = "mm/dd/yyyy hh:mm:ss"
        End With
        
        
        With inputWks
        Set myRng = .Range(myCopy)

        If Application.CountA(myRng) <> myRng.Cells.Count Then
            MsgBox "Please fill in all the cells!"
          
            Exit Sub
        End If
    End With
    
    
    
        .Cells(nextRow, "B").Value = Application.UserName
        oCol = 3
        For Each myCell In myRng.Cells
            historyWks.Cells(nextRow, oCol).Value = myCell.Value
            oCol = oCol + 1
        Next myCell
    End With
    
    'clear input cells that contain constants
    With inputWks
      On Error Resume Next
         With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
              .ClearContents
              Application.GoTo .Cells(1) ', Scroll:=True
            
         End With
      On Error GoTo 0
    End With
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Lets start with this and go from there:
VBA Code:
Sub UpdateLogWorksheet_mod()

    Dim historyWks As Worksheet
    Dim inputWks As Worksheet

    Dim nextRow As Long
    Dim oCol As Long

    Dim myRng As Range
    'Dim myCopy As String        ' XXX Removed not required
    'Dim myCell As Range         ' XXX Removed not required
    
        ' Changed from myCopy

    Set inputWks = Worksheets("Order calculation")
    'cells to copy from Input sheet - some contain formulas
    Set myRng = inputWks.ListObjects("Table1").DataBodyRange
    Set historyWks = Worksheets("Sales History")

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

    With historyWks.Cells(nextRow, "A")
        .Value = Now
        .NumberFormat = "mm/dd/yyyy hh:mm:ss"
    End With
                
    With inputWks
        If Application.CountA(myRng) <> myRng.Cells.Count Then
            MsgBox "Please fill in all the cells!"
          
            Exit Sub
        End If
    End With
    
    With historyWks
        .Cells(nextRow, "B").Value = Application.UserName
        myRng.Copy
        oCol = 3
        .Cells(nextRow, oCol).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    End With
    
    'clear input cells that contain constants
      On Error Resume Next
         With myRng.Cells.SpecialCells(xlCellTypeConstants)
              .ClearContents
              Application.GoTo .Cells(1) ', Scroll:=True
         End With
      On Error GoTo 0

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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