Paste Special VBA does not work in embedded excel workbook - vba

I'm having an issue with the PasteSpecial function when working in an embedded excel workbook. The program I am working in is "Promax" which is block diagramming software running in Visio, which has the option to add an embedded excel workbook. I've essentially set up a number of cells in excel so that I can import a bunch of fields into a PDF form.
While working in the embedded workbook I am unable to get this function to give any output into the new excel worksheet. If I save a version of the workbook that is outside of promax, the code runs fine. If I just try to paste instead of paste special the code works fine, but all of the references that I pasted break in the new workbook.
Sub ExporttotxtFile()
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
Set WorkRng = Sheets("Sheet1").Range("A1:HK2")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wb.Worksheets(1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
saveFile = Application.GetSaveAsFilename(InitialFileName:="Export", fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Does anyone have a solution to this or another method of getting this done? This code was taken from this: https://www.extendoffice.com/documents/excel/612-excel-export-data-to-text.html
Thanks!

While playing around with this more it seemed like xlPasteFormat was pasting an image over the values rather than formatting. I scrapped the pastespecial method and used a different solution.
wb.Worksheets(1).Paste
wb.Worksheets(1).Range("A1:HK2").Value = WorkRng.Value
I paste everything with the first paste and set the values with the second line so that the formatting stays.
Thanks for the help everyone.

Related

Create a macro that copy and pastes data

I'm trying to create a macro that opens a file and copy and pastes data from that file into a new excel sheet.
Problem is, the file is updated every month. So I have a sheet on excel where I have copy pasted the path to that file (Instructions and its in cell A2). I want to know how I can adjust my code to open that file, copy its data and close the file. I also want to create a button that I can press to run the macro.
This is my code so far:
Sub ImportData_Click()
'open the source workbook and select the source sheet
Workbooks.Open Filename:="'Instructions!'$A$2" 'this is the part of the
code that I'm having trouble with
Sheets("ABC").Select
' copy the source range
Sheets("ABC").Range("C:AI").Select
Selection.Copy
' select current workbook and paste the values
ThisWorkbook.Activate
Sheets("ABC DUMP").Select
Sheets("ABC DUMP").Range("A:").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
'close the source workbook
Windows("'Instructions!'$A$2").Activate
ActiveWorkbook.Close
End Sub
You have several minor syntax errors. With A2 data like:
C:\TestFolder\ABC.xls
this appears to work just fine:
Sub ImportData_Click()
'open the source workbook and select the source
Dim wb As Workbook
Workbooks.Open Filename:=Sheets("Instructions").Range("$A$2").Value
Set wb = ActiveWorkbook
Sheets("ABC").Select
' copy the source range
Sheets("ABC").Range("C:AI").Select
Selection.Copy
' select current workbook and paste the values
ThisWorkbook.Activate
Sheets("ABC DUMP").Select
Sheets("ABC DUMP").Range("A1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
'close the source workbook
wb.Close
End Sub
This can be re-coded to avoid Select
If you define a variable as a string and then set it to equal your filename ('Instructions!'$A$2) you can then use this variable in the workbooks.open function.
Your workbooks.open function also requires a pathname for this workbook; therefore define another variable for your pathname and you should be able to use:
Workbooks.Open Filename:=PathName & Filename

VBA MACRO unable to paste

Please help me... this is driving me nuts.
I am trying to copy some data from a CSV but unable to paste it to the destination file that I normally do manually.
The issue I am facing:
-I cannot go back to the destination file's sheet
-Even if I could it would paste as string
-I need the data to be identical from the CSV
MyFile = Application.GetOpenFilename()
ChDir "C:\datafolder\"
Application.Workbooks.Open (MyFile)
Range("A1").CurrentRegion.Select
Selection.copy
ActiveWorkbook.Close savechanges:=False
Application.ScreenUpdating = True
Windows("chickenfeed.xlsm").Activate
ActiveWorkbook.Sheets("Raw Export").Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End sub
You closed the source range before the paste.
Try this and learn the flow...
(Assuming it's "Sheet1" you try to copy data from)
Option Explicit
Sub PasteData()
Dim oSourceWB As Workbook, oTargetWB As Workbook, MyFile As String
MyFile = Application.GetOpenFilename()
ChDir "C:\datafolder\"
On Error Resume Next
Set oSourceWB = Workbooks.Open(Filename:=MyFile, ReadOnly:=True)
Set oTargetWB = Workbooks("chickenfeed.xlsm")
On Error GoTo 0
If Not (oSourceWB Is Nothing And oTargetWB Is Nothing) Then
oSourceWB.Worksheets("Sheet1").Range("A1").CurrentRegion.Copy oTargetWB.Sheets("Raw Export").Range("A1")
oSourceWB.Close SaveChanges:=False
End If
Set oSourceWB = Nothing
Set oTargetWB = Nothing
End Sub

Copy data from a selected excel file

I am trying to construct a macro that when run will allow me to select a given file and check the data in Column C of that selected file. I am very new to VBA and have only rudimentary skills. I have all the parts of my code working except for the portion where I pull in the data from the variable file and paste it into column A of my macro file for the review functions to perform.
I have cobbled together the below code to populate the data from Column C of any given selected file into Column A of the macro file from what I could piece together from searching through the site, but I am still getting an error 400 after selecting the file to open when running this Sub. Would appreciate any assistance with figuring this portion out.
Thanks!
Sub PopulateUploaderFunds()
'Pull in funds from uploader to be reviewed for custody and mirror accounts
Dim uploadfile As Variant
Dim uploader As Workbook
MsgBox ("Please select uploader file to be reviewed")
uploadfile = Application.GetOpenFilename()
If uploadfile = "False" Then
Exit Sub
End If
Workbooks.Open uploadfile
Set uploader = ActiveWorkbook
With uploader
Application.CutCopyMode = False
Range("C1").End(xlDown).Select
Selection.Copy
End With
Windows("Test Mirror Macro Build Test.xlsm").Activate
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
It looks like a problem with how you are navigating between workbooks, try this:
Sub PopulateUploaderFunds()
'Pull in funds from uploader to be reviewed for custody and mirror accounts
Dim uploadfile As Variant
Dim uploader As Workbook
Dim CurrentBook As Workbook
Set CurrentBook = ActiveWorkbook
MsgBox ("Please select uploader file to be reviewed")
uploadfile = Application.GetOpenFilename()
If uploadfile = "False" Then
Exit Sub
End If
Workbooks.Open uploadfile
Set uploader = ActiveWorkbook
With uploader
Application.CutCopyMode = False
Range("C:C").Copy
End With
CurrentBook.Activate
Sheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

Copying from closed workbook

I have been searching for over a hour now and can not find anything that works for this and would appreciate any help at all. I have been using the following code:
Sub copySheet()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").Copy After:=ThisWorkbook.Sheets(1)
srcBook.Close False
End Sub
This copies as expected however, in the copied workbook it creates a new sheet "Sheet1(2)" instead of adding this to the existing sheet1. If repeated it creates "Sheet1(3)", "Sheet1(4)", "Sheet1(5)", etc...
I am really stuck with this and cannot find an answer anywhere.
There are two possible tasks at issue here:
Copying a sheet, which your code does successfully
Copying the contents of a sheet, which is also called a Range
It sounds like what you actually want to do is copy a Range to an existing worksheet. Try this instead:
Sub copySheetContents()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").UsedRange.Copy
ThisWorkbook.Sheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
srcBook.Close False
End Sub
Note that the above assumes that your used range in the source workbook starts at cell A1.
If you are trying to just overwrite what is in your current sheet1:
Sub copySheet()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").UsedRange.Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(1,1)
srcBook.Close False
End Sub

Simple Excel VBA macro failing

I have the following simple macro to copy data from a closed worksheet. The code runs fine from the VBA editor but fails with a subscript error when run from Excel via macro. The paste special statement appears to be the issue.
I just can't see where the problem is, can anyone help?
Dim wsMaster As Worksheet
Set wsMaster = Worksheets("Master Data")
Dim lastrow As Long
Dim Files As String
Files = "Download.xlsx"
Dim filepath As String
filepath = "C:\users\ms612533\desktop\"
Application.ScreenUpdating = False
wsMaster.Activate
Cells.Select
Selection.Clear
Workbooks.Open (filepath & Files)
lastrow = Worksheets("Global").UsedRange.Rows.Count
Worksheets("Global").Range("A1:V" & lastrow).Copy _
wsMaster.Range("B1")
Worksheets("Global").Range("CV1:cv" & lastrow).Copy
wsMaster.Range("a1").PasteSpecial (xlValues)**
Application.CutCopyMode = False
ThisWorkbook.Activate
Call CloseAll
Application.ScreenUpdating = True
End Sub
Sub CloseAll()
' Close all but the active workbook
Dim wkbk As Workbook
Application.ScreenUpdating = False
For Each wkbk In Application.Workbooks
If wkbk.Name <> ActiveWorkbook.Name Then
wkbk.Close SaveChanges:=False
End If
Next
Application.ScreenUpdating = True
End Sub
I think there's something wrong with the PasteSpecial line: when I use the macro recorder, I get something like this:
wsMaster.Range("a1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I think we can ignore the parameters after the first. Then we have this:
wsMaster.Range("a1").PasteSpecial Paste:=xlPasteValues
Note that there are no parens (()) around the arguments: PasteSpecial doesn't return anything so it should be treated like a function. That's probably where the subscript issue is coming from.
Also notice the parameter, which comes from the xlPasteType enum, is a little different from the value you had.
The code appears to work fine when calling the macro from a button, but it doesn't work from a shortcut. I'll put it down to an Excel 'feature' and move on.