Convert VBA unmerge and fil to VBscript unmerge and fill - vba

Hello I am trying to convert VBA to VBscript but having trouble as I do not know where to start. Any help is appreciated.
VBA: Un-merging cells and then filling data to those cells from original merged data cell.
Sub UnMergeFill()
Dim cell As Range, joinedCells As Range
For Each cell In ThisWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
End Sub
VBScript: I have the start of the code to open the correct excel file and save and close but not the modification the VBA script is doing.
'create the excel object
Set objExcel = CreateObject("Excel.Application")
'view the excel program and file, set to false to hide the whole process
objExcel.Visible = True
'open an excel file (make sure to change the location) .xls for 2003 or earlier
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Desktop\vbsTest.xlsx")
'save the existing excel file
objWorkbook.Save
'close the workbook
objWorkbook.Close
'exit the excel program
objExcel.Quit
'release objects
Set objExcel = Nothing
Set objWorkbook = Nothing

VBscript Code:
'Microsoft Excel Automation
':: Open, unmerge cells and fill then save
'---------------------------------
'create the excel object
Set objExcel = CreateObject("Excel.Application")
'view the excel program and file, set to false to hide the whole process
objExcel.Visible = True
'open an excel file (make sure to change the location) .xls for 2003 or earlier
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Desktop\Book3.xlsx")
Dim cell
Dim joinedCells
For Each cell In objWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
'save the existing excel file
objWorkbook.Save
'close the workbook
objWorkbook.Close
'exit the excel program
objExcel.Quit
'release objects
Set objExcel = Nothing
Set objWorkbook = Nothing

Related

how to save macro code as vbs?

I don't know why I can't call my macro code from R, This is my code that I'm trying to save as vbs file: (should I save it in Notepad application?)
Sub vb()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("path.xlsm", 0, False)
xlApp.Visible = True
xlApp.Run "Countries"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
How can I save the above code as vbs ?
You can export a code module from the project explorer, right-click on it and select Export File....
You can also do it with VBA, i.e. to export "Module1":
With ThisWorkbook.VBProject.VBComponents("Module1")
.Export "c:\so\" & .Name & ".bas"
End With
Use the below VBS code as you cannot save VBA code as VBS as the schema or say they are not in the same page as C and C++ are different it is in the same way
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("path.xlsm")
objExcel.Application.Visible = True
objExcel.Application.Run "path.xlsm!Countries" 'Refer to the below if the code is under sheet
objExcel.ActiveWorkbook.Close
WScript.Echo "Finished."
WScript.Quit
If you have placed the code in the sheet. user this line
objExcel.Application.Run "path.xlsm!sheet1.dog"
Hope this resolve your query. Happy Coding.

VBA copying from Excel file to WORD file bookmark

When I copy a chart from Excel ('Report' sheet) to a WORD file ('Report template.docx'), why does VBA wipe out the previous content of the WORD file? I suspect the problem is in line 'wddoc.Range.Paste' but I don't know how to change it to avoid the problem.
Sub ActivateWordTransferData()
Dim wdapp As Object, wddoc As Object
Dim strdocname As String
Set wdapp = GetObject(, "Word.Application")
wdapp.Visible = True
strdocname = "C:\users\ian\Documents\Dropbox\Report template.docx"
Set wddoc = wdapp.documents(strdocname)
Worksheets("Report").Shapes("Chart 2").Copy
wdapp.Activate
wddoc.bookmarks("bkmark4").Select
wddoc.Range.Paste
wddoc.Save
Set wddoc = Nothing
Set wdapp = Nothing
Application.CutCopyMode = False
End Sub
I'm not sure why the contents of the Word document are being overwritten.
However, removing the .Select operation and just pasting into the bookmark's range seems to work.
Remove these lines:
wddoc.bookmarks("bkmark4").Select
wddoc.Range.Paste
and replace with this line:
wddoc.bookmarks("bkmark4").Range.Paste

Bring Excel file to front when running Outlook VBA code

I wrote a macro in Outlook to set the value of a cell in Excel file which is opening.
Sub test()
Dim objExcel As Object, WB As Object, WS As Object
Set objExcel = GetObject(, "Excel.Application")
objExcel.Visible = True
Set WB = objExcel.Workbooks("Book1.xlsm")
WB.Activate
Set WS = WB.Worksheets("Sheet1")
AppActivate "Microsoft Outlook"
WS.Range("A1").Value = "hoho"
End Sub
It sets the value of the cell, but I still stand in Outlook.
How can I display the Excel file instead of Outlook?
The way to do that is to minimize the Application Window and then maximize it.
objExcel.WindowState = xlMinimized
objExcel.WindowState = xlMaximized

File copies and pastes blank data (or does not even copy and paste). Also loading is long

can someone please tell me why the data is not copying and pasting (or why it is copying and pasting blank data? Also is there a way to speed the automation?
Sub GetDataCopyPaste()
Dim wbTarget As Workbook 'where the data will be pasted
Dim wbSource As Workbook 'where the data will be copied
Dim StrName As String 'name of the source sheet
Application.ScreenUpdating = False 'these statements help performance by disabling the self titled in each, remeber to re-enable at end of code
Application.DisplayAlerts = False
Set wbTarget = ActiveWorkbook 'set to the current workbook
StrName = ActiveSheet.Name 'get active sheetname of workbook
Set wbSource = Workbooks.Open("C:\Users\jjordan\Desktop\Test Dir\Test File Test\metrics list.xlsx") 'opens Target workbook
Set wbTarget = Workbooks.Open("C:\Users\jjordan\Desktop\Test Dir\MASTER\Weekly Logbook - 2016.xlsm") 'opens Source workbook
wbSource.Sheets("IOS").Range("A1:E60").Value = wbTarget.Sheets("Sheet6").Range("A1:E60").Value 'copy & pastes source data onto Target workbook
wbTarget.Save 'save workbook
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
This line is backwards
wbSource.Sheets("IOS").Range("A1:E60").Value = wbTarget.Sheets("Sheet6").Range("A1:E60").Value 'copy & pastes source data onto Target workbook
You need
wbTarget.Sheets("Sheet6").Range("A1:E60") = wbSource.Sheets("IOS").Range("A1:E60").value
I just tested and succeeded
Option Explicit
Sub test()
Dim myWB As Workbook
Set myWB = Workbooks.Open("C:\Users\raystafarian\Downloads\Book3.xlsx")
Dim yourWB As Workbook
Set yourWB = Workbooks.Open("C:\Users\raystafarian\Downloads\Book2.xlsm")
myWB.Sheets("Sheet1").Range("C1:C4").Value = yourWB.Sheets("Sheet1").Range("A1:A4").Value
End Sub

How to open a powerpoint chart backend sheet in new instance

I am updating a powerpoint presentation using vba.
There are 30 charts in the ppt and I will be coping the data from some excel sheets to the backend excel sheets of the powerpoint charts. but each time I do it, I can see the new excel sheets getting opened in the taskbar. even though they don't display on the whole screen
Set CExcel = New Excel.Application
CExcel.Visible = False
Set CWB2 = CExcel.Workbooks.Open(PPPres.Slides(lngSldNo).Shape(strChartName).Chart.ChartData.Workbook)
This is the code I am using, but it is giving me error that you can not open a file like this
can you tell me how the backend sheets of powerpoint charts can be opened in a new instance and without displaying the file and its tab also
Thanks in advance
The 1st argument to the Workbooks.Open method is a string containing the file path, e.g.
Sub Test()
Dim EAPP As Excel.Application, EWB As Excel.Workbook
Set EAPP = New Excel.Application
Set EWB = EAPP.Workbooks.Open("C:\Users\Myself\Desktop\MyFile.xlsx")
Set EWB = Nothing
Set EAPP = Nothing
End Sub
Not sure that is delivered by your argument. The Application.Visible property by default is False, so no need to explicitely set it. And I guess you have created a reference to the Excel Object Library to make use of the Excel objects in Powerpoint for early binding.
Logic:
Get the shape which has the Chart object
Get your Chart object and then Get the Chartdata
Launch the chart and set your workbook and your Excel application objects
Assumptions:
For demonstration purpose I am assuming the following
Slide1 in the presentation has one shape which is a chart
I have added both scenarios. i.e Automating Excel from PowerPoint and Automating PowerPoint from Excel
Is this what you are trying?
Code: Automating Excel from PowerPoint
Option Explicit
Sub Sample()
'~~> Excel Objects
Dim oXlApp As Object, oXlWb As Object, oXlSheet As Object
'~~~> Powerpoint objects
Dim oPPChart As Chart
Dim oPPChartData As ChartData
'~~> Working with shape1
With ActivePresentation.Slides(1).Shapes(1)
If .HasChart Then
Set oPPChart = .Chart
Set oPPChartData = oPPChart.ChartData
oPPChartData.Activate
'~~> Set your Excel objects here
Set oXlWb = oPPChartData.Workbook
Set oXlApp = oXlWb.Parent
oXlApp.Visible = False
Debug.Print oXlApp.Name
Debug.Print oXlWb.Name
'
'~~> Rest of your code
'
End If
End With
'~~> Close And Cleanup
oXlWb.Close False
oXlApp.Quit
Set oXlSheet = Nothing
Set oXlWb = Nothing
Set oXlApp = Nothing
End Sub
CODE: Automating PowerPoint from Excel
Option Explicit
Sub Sample()
'~~> Excel Objects
Dim oXlApp As Application, oXlWb As Workbook, oXlSheet As Worksheet
'~~~> Powerpoint objects
Dim oPPApp As Object, oPPprsn As Object, oPPSlide As Object
Dim oPPChart As Object, oPPChartData As Object
Application.ScreenUpdating = False
'~~> Establish a Popwerpoint application object
On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oPPApp = CreateObject("PowerPoint.Application")
End If
Err.Clear
On Error GoTo 0
'~~> open relevant powerpoint file
Set oPPprsn = oPPApp.Presentations.Open("C:\Presentation1.pptx")
Set oPPSlide = oPPprsn.Slides(1)
'~~> Working with shape1
With oPPSlide.Shapes(1)
If .HasChart Then
Set oPPChart = .Chart
Set oPPChartData = oPPChart.ChartData
oPPChartData.Activate
'~~> Set your Excel objects here
Set oXlWb = oPPChartData.Workbook
Set oXlApp = oXlWb.Parent
'~~> This is required if powerpoint chartdata
'~~> opens in the same instance of this excel
oXlWb.Windows(1).Visible = False
'oXlApp.Visible = False
Debug.Print oXlApp.Name
Debug.Print oXlWb.Name
'
'~~> Rest of your code
'
End If
End With
oXlWb.Windows(1).Visible = True
'~~> Close And Cleanup
oXlWb.Close False
oPPprsn.Close
oPPApp.Quit
Application.ScreenUpdating = True
Set oPPChartData = Nothing
Set oPPChart = Nothing
Set oPPChartData = Nothing
Set oPPSlide = Nothing
Set oPPprsn = Nothing
Set oPPApp = Nothing
End Sub