Help Finishing this VBA Code

mrbcodc87

New Member
Joined
May 19, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I Have two parts of code i need to put together and add too i need help with making at all work.

The first part of code selects cells D10 across to K10, copies it to another sheet i am using as a database in the last line of the sheet, then clears the original data i have copied from the sheet

Sub SaveLineData()
'
' SaveLineData Macro
'

'
Range("D10:K10").Select
Selection.Copy
Sheets("Database").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SCADA Entry Form").Select
Range("F10:K10").Select
Selection.ClearContents
End Sub

the second part of code needs to copy the data from cells D10 across to K10 to a text file along with the username of the use logged in running, then save the text file as the data from cell e10 to my network drive.

Sub Foo()
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim wbDest As Workbook
Dim fName As String
Dim TextFile As Integer
Dim FilePath As String

Set wbSource = ActiveWorkbook
Set wsSource = ActiveSheet

wsSource.Range("d10:k10").Copy

Shell "notepad.exe", vbNormalFocus

SendKeys "^V"
SendKeys "^{HOME}"


End Sub

I have got the code to open notepad and paste the cells but i am struggling to get it to write the user name, save as (data from e10) then close

if someone could combine these codes and help me finish what i am trying to do that would be great

thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try something like this:

VBA Code:
Sub Test()
    Dim rng As Range
    Dim outputstring As String
    Dim rCell As Range
    Dim lFile As Long
    
    Set rng = ThisWorkbook.Worksheets("SCADA Entry Form").Range("D10:K10")
        
    'Copy data values to Database sheet.
    With ThisWorkbook.Worksheets("Database")
        .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, rng.Columns.Count) = rng.Value
    End With
    
    'Convert the values in the cells to a single text string.
    For Each rCell In rng
        outputstring = outputstring & rCell.Value & " - "
    Next rCell
    
    rng.ClearContents
    
    'Output data to text file in same folder as Excel file.
    'Date, time, user name and data is appended to text file.
    lFile = FreeFile
    Open ThisWorkbook.Path & "\Data Log.txt" For Append As #lFile
    Print #lFile, Now() & " " & Environ("username") & " - " & outputstring
    Close #lFile
End Sub
 
Upvote 0
Try something like this:

VBA Code:
Sub Test()
    Dim rng As Range
    Dim outputstring As String
    Dim rCell As Range
    Dim lFile As Long
   
    Set rng = ThisWorkbook.Worksheets("SCADA Entry Form").Range("D10:K10")
       
    'Copy data values to Database sheet.
    With ThisWorkbook.Worksheets("Database")
        .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, rng.Columns.Count) = rng.Value
    End With
   
    'Convert the values in the cells to a single text string.
    For Each rCell In rng
        outputstring = outputstring & rCell.Value & " - "
    Next rCell
   
    rng.ClearContents
   
    'Output data to text file in same folder as Excel file.
    'Date, time, user name and data is appended to text file.
    lFile = FreeFile
    Open ThisWorkbook.Path & "\Data Log.txt" For Append As #lFile
    Print #lFile, Now() & " " & Environ("username") & " - " & outputstring
    Close #lFile
End Sub


Thanks that works great!
How can i change the path to where the .txt file is saved?
I want it to be in a folder I have created ("O:\Scada Logs")
 
Upvote 0
Just change
VBA Code:
ThisWorkbook.Path & "\Data Log.txt"
to the path you want. E.g
Code:
"C:\Somefolder\Filename.txt
.
 
Upvote 0
Thanks for your help!
I have the final bit of vba code I need for the spreadsheet I need help with if you could help me with if didn’t mind?

 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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