In this site, I found code that it prints the last slide as PDF.
Sub PDFtesti()
timestamp = Now()
Dim PR As PrintRange
Dim lngLast As Long
Dim lngFirst As Long
Dim savePath As String
Dim PrintPDF As Integer
Dim name As String
name = ActivePresentation.Slides(2).Shapes("TextBox1").OLEFormat.object.Text
savePath = "C:\Powerpoint\" & Format(timestamp, "yyyymmdd-hhnn") & " - " & name & ".pdf"
lngLast = ActivePresentation.Slides.Count
With ActivePresentation.PrintOptions
.Ranges.ClearAll
Set PR = .Ranges.Add(lngLong, lngLong)
End With
ActivePresentation.ExportAsFixedFormat _
Path:=savePath, _
FixedFormatType:=ppFixedFormatTypePDF, _
PrintRange:=PR, _
Intent:=ppFixedFormatIntentScreen, _
FrameSlides:=msoTrue, _
RangeType:=ppPrintSlideRange
End Sub
I would like print two slides: slide number 2 and last page.
I tried
Set PR = .Ranges.Add(lngLong, lngLong)
Set PR = .Ranges.Add(2, 2)
and
Set PR = .Ranges.Add(Array("lngLong, lngLong" & "2,2")
Updated solution. In order to make printing specific slides more comfortable, I decided to put them into one variable (slidesToPrint). All slides not put into this variable are hidden just before printing (so they are not printed). After printing the hide order is restored to the original. So, slides which must be printed, bust be listed in this line:
slidesToPrint = Array(2, lngLast)
Full code:
Sub PDFtesti()
Dim timestamp As Date
Dim PR As PrintRanges
Dim lngLast As Long
Dim lngFirst As Long
Dim savePath As String
Dim PrintPDF As Integer
Dim name As String
Dim originalHides() As Long
Dim slidesToPrint() As Variant
Dim i As Variant
timestamp = Now()
With ActivePresentation
name = .Slides(2).Shapes("TextBox1").OLEFormat.Object.Text
savePath = "C:\Powerpoint\" & Format(timestamp, "yyyymmdd-hhnn") & " - " & name & ".pdf"
lngLast = .Slides.Count
.PrintOptions.Ranges.ClearAll
' Slides to print are put here (inside parentheses)
slidesToPrint = Array(2, lngLast)
ReDim originalHides(1 To lngLast)
For i = 1 To lngLast
originalHides(i) = .Slides(i).SlideShowTransition.Hidden
.Slides(i).SlideShowTransition.Hidden = -1
Next
For Each i In slidesToPrint()
.Slides(i).SlideShowTransition.Hidden = 0
Next
.ExportAsFixedFormat _
Path:=savePath, _
FixedFormatType:=ppFixedFormatTypePDF, _
Intent:=ppFixedFormatIntentScreen, _
FrameSlides:=msoTrue
For i = 1 To lngLast
.Slides(i).SlideShowTransition.Hidden = originalHides(i)
Next
End With
End Sub
In your code you use PR As PrintRange. But since you want two pages, the second and the last, you will need two ranges, which is not of type PrintRange, but instead, PrintRanges. In this case you would do:
.Ranges.Add(2, 2)
.Ranges.Add(lngLast, lngLast)
But this would not work, because the function ExportAsFixedFormat only accepts PrintRange, but not PrintRanges. One option would be printing both slides to separate files first using PrintRanges(1) i.e. (2, 2) and next using PrintRanges(2) i.e. (lngLast, lngLast). But this is not really what you want.
The solution. It uses PR(1) (PR is of type PrintRanges, while PR(1) is PrintRange). PR(1) is the range corresponding to the last two slides. The trick is that just before printing you move the second slide to the position lngLast - 1 (one before last) and after printing you return it to the correct place.
This moves the second slide to one before last position:
.Slides(2).MoveTo lngLast - 1
This returns it to the original position:
.Slides(lngLast - 1).MoveTo 2
Full code:
Sub PDFtesti()
Dim timestamp As Date
Dim PR As PrintRanges
Dim lngLast As Long
Dim lngFirst As Long
Dim savePath As String
Dim PrintPDF As Integer
Dim name As String
timestamp = Now()
With ActivePresentation
name = .Slides(2).Shapes("TextBox1").OLEFormat.Object.Text
savePath = "C:\Powerpoint\" & Format(timestamp, "yyyymmdd-hhnn") & " - " & name & ".pdf"
lngLast = .Slides.Count
Set PR = .PrintOptions.Ranges
PR.ClearAll
PR.Add lngLast - 1, lngLast
.Slides(2).MoveTo lngLast - 1
.ExportAsFixedFormat _
Path:=savePath, _
FixedFormatType:=ppFixedFormatTypePDF, _
PrintRange:=PR(1), _
Intent:=ppFixedFormatIntentScreen, _
FrameSlides:=msoTrue, _
RangeType:=ppPrintSlideRange
.Slides(lngLast - 1).MoveTo 2
End With
End Sub
Related
Any help here would be appreciated please.
The included VBA code almost meets the intended purpose, however, I need a solution that enables the use of wildcards and highlights all parameters contained between "##", "%%" or potentially other special characters (special characters included).
For instance, lets say in the cell range B2:B10 we would find something like:
Checked at ##date1## and ##hour1##
But I want to be able to do a search and highlight using # * # or % * % within a selected determined cell range with the end result (bold being color):
Checked at ##date1## and ##hour1##
Sub HighlightStrings()
Application.ScreenUpdating = False
Dim Rng As Range
Dim cFnd As String
Dim xTmp As String
Dim x As Long
Dim m As Long
Dim y As Long
Dim xFNum As Integer
Dim xArrFnd As Variant
Dim xStr As String
cFnd = InputBox("Please enter the text, separate them by comma:")
If Len(cFnd) < 1 Then Exit Sub
xArrFnd = Split(cFnd, ",")
For Each Rng In Selection
With Rng
For xFNum = 0 To UBound(xArrFnd)
xStr = xArrFnd(xFNum)
y = Len(xStr)
m = UBound(Split(Rng.Value, xStr))
If m > 0 Then
xTmp = ""
For x = 0 To m - 1
xTmp = xTmp & Split(Rng.Value, xStr)(x)
.Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
xTmp = xTmp & xStr
Next
End If
Next xFNum
End With
Next Rng
Application.ScreenUpdating = True
End Sub
Thank you
Okay This seems to work for me. There is a limitation we can work on if required: the phrase to highlight must be padded with spaces on both sides.
Option Explicit
Option Base 0
Sub testreplace()
Dim I As Integer 'Iteration
Dim FlagNum As Integer 'Flag Number
Dim RG As Range 'Whole range
Dim CL As Range 'Each Cell
Dim FlagChar As String 'Flag characters
Dim ArrFlag 'Flag Char Array
Dim TextTemp As String 'Cell Contents
Set RG = Selection
FlagChar = "##"
FlagChar = InputBox("Enter 'Flag Characters' separated by a comma." & vbCrLf & vbCrLf & _
"Example:" & vbCrLf & vbCrLf & _
"##,%%,&&" & vbCrLf & _
"$$,XX", "Flag Characters to Highlight", "##,%%")
ArrFlag = Split(FlagChar, ",")
For Each CL In RG.Cells
TextTemp = CL.Value
For FlagNum = 0 To UBound(ArrFlag)
For I = 1 To Len(TextTemp)
'Debug.Print "<<" & Mid(TextTemp, I, Len(ArrFlag(Flagnum)) + 1) & _
"=" & " " & ArrFlag(Flagnum) & ">>"
If Mid(TextTemp, I, Len(ArrFlag(FlagNum)) + 1) = " " & ArrFlag(FlagNum) Then
CL.Characters(I + 1, InStr(I, TextTemp, ArrFlag(FlagNum) & " ") + _
Len(ArrFlag(FlagNum)) - I).Font.ColorIndex = 3
End If
Next I
Next FlagNum
Next CL
End Sub
Here's an example of it working:
I found this code, which add's one extra column to the chart each time it runs.
Meaning first time it runs it shows week 1-7, secound time 1-8, next 1-9 and I would like it to show 2-7, 3-8, 4-9 ect.
Sub ChartRangeAdd()
On Error Resume Next
Dim oCht As Chart, aFormulaOld As Variant, aFormulaNew As Variant
Dim i As Long, s As Long
Dim oRng As Range, sTmp As String, sBase As String
Set oCht = ActiveSheet.ChartObjects(1).Chart
oCht.Select
For s = 1 To oCht.SeriesCollection.count
sTmp = oCht.SeriesCollection(s).Formula
sBase = Split(sTmp, "(")(0) & "(<FORMULA>)" ' "=SERIES(" & "<FORMULA>)"
sTmp = Split(sTmp, "(")(1) ' "..., ..., ...)"
aFormulaOld = Split(Left(sTmp, Len(sTmp) - 1), ",") ' "..., ..., ..."
aFormulaNew = Array()
ReDim aFormulaNew(UBound(aFormulaOld))
' Process all series in the formula
For i = 0 To UBound(aFormulaOld)
Set oRng = Range(aFormulaOld(i))
' Attempt to put the value into Range, keep the same if it's not valid Range
If Err.Number = 0 Then
Set oRng = oRng.Worksheet.Range(oRng, oRng.Offset(0, 1))
aFormulaNew(i) = oRng.Worksheet.Name & "!" & oRng.Address
Else
aFormulaNew(i) = aFormulaOld(i)
Err.Clear
End If
Next i
sTmp = Replace(sBase, "<FORMULA>", Join(aFormulaNew, ","))
Debug.Print "Series(" & s & ") from """ & oCht.SeriesCollection(s).Formula & """ to """ & sTmp & """"
oCht.SeriesCollection(s).Formula = sTmp
sTmp = ""
Next s
Set oCht = Nothing
End Sub
I want to do the opposite of this code, so instead of adding a column one column should be substracted. How can the code be modifued to do this?
(LINK: VBA: Modify chart data range)
Thank you!
Try changing the line
Set oRng = oRng.Worksheet.Range(oRng, oRng.Offset(0, 1))
to
Set oRng = oRng.Worksheet.Range(oRng.Offset(0, 1), oRng.Offset(0, 1))
I am using a macro and VBA code to create a text file with a specific format. All the data needed to create the text file is gathered from the macro cells.
I have attached pictures of the macro data file and the output text file (please see below).
excel macro with data
Desired output txt format-example
Also, below is my VBA code I generated to get data from the macro and create/write into a text file. I still need to figure out how to write it in the specified format (Desired output txt format-example).
Sub ExcelToTxt()
'Declaring variables
Dim lCounter As Long
Dim lLastRow As Long
Dim destgroup As String
Dim parmlabel as Variant
Dim FName As Variant
'Activate Sheet1
Sheet1.Activate
'Find the last row that contains data
With Sheet1
lLastRow = .Cells(.Rows.Count, "A").End(xlDown).Row
End With
'Create txt file
FName = Application.GetSaveAsFilename("", "txt file (*.txt), *.txt")
'Open FName For Output As #1
For lCounter = 2 To lLastRow
'Read specific data from the worksheet
With Sheet1 destgroup = .Cells(lCounter, 19)
parmlabel = .Cells(lCounter, 8)
If destgroup="trex_15hz" Or destgroup="trex_10hz" Or destgroup="trex_5hz" Then
'Write selected data to text file
'Write #1, parmlabel
End If
End With
'Continue looping until the last row
Next lCounter
'Close the text file
Close #1
End Sub
Any help with what I need to add in my VBA to create the formatted output txt file will be greatly appreciate it.
Thank you in advance.
You can combine the data into an array and then convert it back into text.
Sub ExcelToTxt()
'Declaring variables
Dim i As Long, j As Integer
Dim n As Long, k As Long
Dim destgroup As String
Dim FName As String
Dim vDB, vR(1 To 6), vJoin(), vResult()
Dim sJoin As String, sResult As String
Dim s As Long
'Activate Sheet1
Sheet1.Activate
'Find the last row that contains data
With Sheet1
vDB = .Range("a1").CurrentRegion '<~~ get data to array from your data range
n = UBound(vDB, 1) 'size of array (row of 2 dimension array)
End With
'Create txt file
FName = Application.GetSaveAsFilename("", "txt file (*.txt), *.txt")
For i = 2 To n '<~~loop
destgroup = vDB(i, 2) '<~~ second column
If destgroup = "trex_15hz" Or destgroup = "trex_10hz" Or destgroup = "trex_5hz" Then
vR(1) = "; ### LABEL DEFINITION ###" '<~~ text 1st line
s = Val(Replace(vDB(i, 3), "label", ""))
vR(2) = "EQ_LABEL_DEF,02," & Format(s, "000")
vR(3) = "UDB_LABEL," & Chr(34) & vDB(i, 4) & Chr(34) '<~~ 2nd line
ReDim vJoin(4 To 7)
vJoin(4) = Chr(34) & vDB(i, 4) & Chr(34)
For j = 5 To 7
vJoin(j) = vDB(i, j)
Next j
sJoin = Join(vJoin, ",")
vR(4) = "STD_SUB_LABE," & sJoin '<~~ 3th line
ReDim vJoin(8 To 12)
vJoin(8) = Chr(34) & UCase(vDB(i, 8)) & Chr(34)
vJoin(9) = Chr(34) & vDB(i, 9) & Chr(34)
vJoin(10) = Format(vDB(i, 10), "#.000000000")
For j = 11 To 12
vJoin(j) = vDB(i, j)
Next j
sJoin = Join(vJoin, ",")
vR(5) = "STD_SUB_LABE," & sJoin '<~~ 4the line
vR(6) = "END_EQ_LABEL_DEF" '<~~ 5th line
k = k + 1
ReDim Preserve vResult(1 To k)
vResult(k) = Join(vR, vbCrLf) '<~~ 5 line in array vR and get to array vResult with join method
End If
Next i
sResult = "EQUIPMENT_ID_DEF,02,0x1," & Chr(34) & "trex" & Chr(34) '<~~ text file first line
sResult = sResult & vbCrLf & Join(vResult, vbCrLf) '<~~ combine 1th and other line
ConvertText FName, sResult '<~~ sub presedure
End Sub
Sub ConvertText(myfile As String, strTxt As String)
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
'.Charset = "utf-8"
.Open
.WriteText strTxt
.SaveToFile myfile, 2
.Close
End With
Set objStream = Nothing
End Sub
Option Explicit
Dim mySheets As Dictionary
Private Sub SaveAndOpen_Click()
'set up variables
Dim i As Long
Dim j As Long
Dim myArr() As Long
Dim filename As String
ReDim myArr(1 To Sheets.Count)
j = 1
'make bounds
Dim from As Long
Dim tonum As Long
'numbers inputted from a userform
from = FromBox.Value
tonum = ToBox.Value
filename = Cells(3, 4) & "." & mySheets.Item(from) & "-" & mySheets.Item(tonum)
For i = 1 To mySheets.Count
If i >= FromBox.Value And i <= ToBox.Value Then
myArr(j) = i
j = j + 1
End If
Next i
Dim filepath As String
For i = 1 To UBound(myArr)
filepath = filepath & myArr(i)
Next i
filepath = "c:\file\path\here\"
ThisWorkbook.Sheets(myArr).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
filepath & filename, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
ThisWorkbook.Sheets(1).Select
End Sub
Private Sub UserForm_Initialize()
Copies.Value = 1
FromBox.Value = 1
Dim i As Long
Set mySheets = New Dictionary
For i = 1 To ActiveWorkbook.Sheets.Count
mySheets.Add i, ActiveWorkbook.Sheets(i).Name
SheetBox.Value = SheetBox.Value & i & " - " & ActiveWorkbook.Sheets(i).Name & vbCrLf
Next i
ToBox.Value = i - 1
End Sub
This subroutine takes information from a userform, which has user inputted variables in FromBox and ToBox; these are both longs. The goal is to be able to save, for instance, sheets 2 - 10. The parameters are specified by the user.
the following code, with the bottom section uncommented, works when the user specifies all of the worksheets (IE there are 10 worksheets, and the user specifies range 1-10). But when the user specifies 2-10, it fails.
The problem, I think, is that I'm trying to select 10 elements with a 9 element long array.
As Scott Holtzman pointed out in a comment, you are dimensioning myArr larger than it should be. It therefore has unassigned values in it, which are left as the default zero value, and that causes problems because you don't have a sheet 0 to be selected.
I think the following code should work:
Option Explicit
Dim mySheets As Dictionary
Private Sub SaveAndOpen_Click()
'set up variables
Dim i As Long
Dim j As Long
Dim myArr() As Long
Dim filename As String
'make bounds
Dim from As Long
Dim tonum As Long
'numbers inputted from a userform
from = FromBox.Value
tonum = ToBox.Value
'Check ToBox.Value is valid
If tonum > Sheets.Count Then
MsgBox "Invalid To value"
Exit Sub
End If
'Check FromBox.Value is valid
If from > tonum Then
MsgBox "Invalid From value"
Exit Sub
End If
'Setup myArr
ReDim myArr(from To tonum)
For j = from To tonum
myArr(j) = j
Next
filename = Cells(3, 4) & "." & mySheets.Item(from) & "-" & mySheets.Item(tonum)
'
Dim filepath As String
'For i = LBound(myArr) To UBound(myArr)
' filepath = filepath & myArr(i)
'Next i
filepath = "c:\file\path\here\"
ThisWorkbook.Sheets(myArr).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
filepath & filename, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
ThisWorkbook.Sheets(1).Select
End Sub
I am trying to archive data from formatted worksheet called BOD_Labsheet to one called Data. I have done something similar using UserForms but am encountering problems here.
When I run the macro, I get the error "Method 'Range' of object _Worksheet failed" on line
dataWorksheet.Cells(emptyRow, 2) = bodWorksheet.Range("BOD_Lab_Date").Value
The Data worksheet is active when I do the copying.
Should I simply copy all the values from BOD_Labsheet to an array, activate the Data Worksheet and recopy values?
Here is the complete code:
Sub Submit_BOD()
'
' Submit_BOD Macro
'
Dim dataWorksheet As Worksheet, bodWorksheet As Worksheet, suspendedSolidsWorksheet As Worksheet
Dim dataSheetName As String
Dim bodSheetName As String
Dim suspendedSolidsName As String
dataSheetName = "Data"
bodSheetName = "BOD_Labsheet"
suspendedSolidsName = "Suspended_Solids_Labsheet"
Set dataWorksheet = ActiveWorkbook.Sheets(dataSheetName)
Set bodWorksheet = ActiveWorkbook.Sheets(bodSheetName)
Set suspendedSolidsWorksheet = ActiveWorkbook.Sheets(suspendedSolidsName)
Dim myRanges() As Variant
myRanges = Array("BOD_Collected_By", "BOD_Temp_Out", "BOD_Temp_IN", "BOD_Source", "BOD_Sample_Vol_4", _
"BOD_Dilution_1", "BOD_Blank_IDO_4", "BOD_Blank_FDO_4", "BOD_Sample_Vol_7", "BOD_Dilution_2", _
"BOD_Blank_IDO_7", "BOD_Blank_FDO_7", "BOD_Seed_IDO_13", "BOD_Seed_FDO_13", "BOD_Seed_IDO_14", _
"BOD_Seed_FDO_14", "BOD_Influent_IDO_15", "BOD_Influent_FDO_15", "BOD_Influent_IDO_16", _
"BOD_Influent_FDO_16", "BOD_Effluent_IDO_20", "BOD_Effluent_FDO_20", "BOD_Effluent_IDO_21", "BOD_Effluent_FDO_21", _
"In_BOD_Concentration", "Out_BOD_Concentration")
'Make Data Sheet active
dataWorksheet.Activate
Dim myDate As Date
myDate = DateValue(bodWorksheet.Range("BOD_Lab_Date").Value)
Dim yearAsString As String, monthAsString As String, dayAsString As String
yearAsString = Format(myDate, "yyyy")
monthAsString = Format(myDate, "mm")
dayAsString = Format(myDate, "dd")
Dim reportNumberText As String
reportNumberText = "NP" & yearAsString & monthAsString & dayAsString
Debug.Print "reportNumberText = "; reportNumberText
'Determine emptyRow
Dim emptyRow As Integer
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
'Sample Number
dataWorksheet.Cells(emptyRow, 1).Value = reportNumberText
'Date and Time Collected
dataWorksheet.Cells(emptyRow, 2) = bodWorksheet.Range("BOD_Lab_Date").Value
dataWorksheet.Cells(emptyRow, 3) = Format(bodWorksheet.Range("BOD_Collection_Date").Value, "dd-mmm-yyyy")
dataWorksheet.Cells(emptyRow, 4) = Format(bodWorksheet.Range("BOD_Read_On_Date").Value, "dd-mmm-yyyy")
Dim i As Integer, j As Integer
For i = LBound(myRanges) To UBound(myRanges)
j = i + 4
dataWorksheet.Cells(emptyRow, j) = bodWorksheet.Range(myRanges(i)).Value
Debug.Print "dataWorksheet.Cells(" & emptyRow & "," & j & ") " & dataWorksheet.Cells(emptyRow, j).Value
Next i
ActiveWorkbook.Save
suspendedSolidsWorksheet.Activate
Range("SS_Date").Select
End Sub
Is "BOD_LAB_DATE" more than one cell? Maybe your method usually works also, but I usually would copy a range of cells by reversing your order and using copy, like so:
bodWorksheet.Range("BOD_Lab_Date").Copy dataWorksheet.Cells(emptyRow, 2)