Unwanted double quote in first line of file being written out - excel

I am writing a to a txt file and the first character in the first line, when I view the file in notepad is a double quote. If I examine the variables that get written out when the code is running, the double quote is not there. Any ideas what might be wrong? Here is my code snippet:
mydata1 = "!TRNS" & vbTab & vbTab & "TRNSID" & vbTab & "TRNSTYPE" & vbTab &
"DATE" & vbTab & "ACCNT" & vbTab & "CLASS" & vbTab & "AMOUNT" & vbTab &
"DOCNUM" & vbTab & "MEMO" & vbCrLf
mydata1 = mydata1 & "!SPL" & vbTab & "SPLID" & vbTab & "TRNSTYPE" & vbTab &
"DATE" & vbTab & "ACCNT" & vbTab & "CLASS" & vbTab & "AMOUNT" & vbTab &
"DOCNUM" & vbTab & "MEMO" & vbCrLf
mydata1 = mydata1 & "!ENDTRNS" & vbTab & vbTab & vbTab & vbTab & vbTab &
vbTab & vbTab & vbTab & vbCrLf
mydata1 = mydata1 & "TRNS" & vbTab & "GENERAL JOURNAL" & vbTab & myDate &
vbTab & "ACCOUNTS REC-CORP OFFICE" & vbTab & "CORPORATE OFFICE" & vbTab &
mytot & vbTab & myDate & vbTab & vbCrLf
mydata1 = mydata1 & mydata2
file = "d:\junk\todd\tester.txt"
Open myfile For Output As #1
Write #1, mydata1
Close #1

You can use below syntax and see if it works for you:
Print #1, mydata1
BTW, the variable myfile and file do not match in your code.

Related

Autofill Range in Excel Using VBA [duplicate]

This question already has answers here:
Better way to find last used row
(9 answers)
Closed 10 months ago.
I've created a program that is supposed to select an entire column and autofill it to the end starting with a formula that will ALWAYS be in a fixed spot on the column. However, the issue is that in terms of row numbers, the number changes daily, so I can't hardcode an ending range.
I tried to do this:
Range("W11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R01""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R01-PO QTY]]="""",0,[#[R01-PO QTY]])" & Chr(10) & " +IF([#[R01-ALC QTY]]="""",0,[#[R01-ALC QTY]])" & Chr(10) & " +IF([#[R01-JOB QTY]]="""",0,[#[R01-JOB QTY]])" & Chr(10) & " +IF([#[R01-GIT QTY]]="""",0,[#[R01-GIT QTY]])," & Chr(10) & "R[-1]C)"
Range("W11").Select
Selection.AutoFill Destination:=Range("W11:W")
But it returns a "Method Range of Object _Global failed.
What do I do instead? How do I select the entire column AFTER W11 in this case?
I got this working. If anyone is wondering what I did, I did it in a sort of roundabout way. I first created a function called GetLastRow
Function GetLastRow(Strt As String, Cur As String)
'''''
'Get the row of the last line of data
'''''
GetLastRow = Range(Strt, Range(Cur).End(xlDown)).Rows.Count
If GetLastRow > 1000000 Then
GetLastRow = ElimAlpha(Cur)
End If
End Function
I then created some Dims to get the last row for EACH of my columns. Worked beautifully.
Sub RunningBalances()
Dim NumRows01 As Integer
Dim NumRows02 As Integer
Dim NumRowsRDS As Integer
Dim NumRows1 As Integer
Dim NumRowsPDS As Integer
'
' RunningBalances Macro
'
'
'CompanyR W/H 01
NumRows01 = GetLastRow("W11", "W11" & VBA.CStr(StrtPOs))
Range("W11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R01""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R01-PO QTY]]="""",0,[#[R01-PO QTY]])" & Chr(10) & " +IF([#[R01-ALC QTY]]="""",0,[#[R01-ALC QTY]])" & Chr(10) & " +IF([#[R01-JOB QTY]]="""",0,[#[R01-JOB QTY]])" & Chr(10) & " +IF([#[R01-GIT QTY]]="""",0,[#[R01-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("W11" & ":W" & NumRows01)
'CompanyR W/H 02
NumRows02 = GetLastRow("AF11", "AF11" & VBA.CStr(StrtPOs))
Range("AF11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R02""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R02-PO QTY]]="""",0,[#[R02-PO QTY]])" & Chr(10) & " +IF([#[R02-ALC QTY]]="""",0,[#[R02-ALC QTY]])" & Chr(10) & " +IF([#[R02-JOB QTY]]="""",0,[#[R02-JOB QTY]])" & Chr(10) & " +IF([#[R02-GIT QTY]]="""",0,[#[R02-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AF11" & ":AF" & NumRows02)
'CompanyR W/H DS
NumRowsRDS = GetLastRow("AO11", "AO11" & VBA.CStr(StrtPOs))
Range("AO11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""RDS""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[RDS-PO QTY]]="""",0,[#[RDS-PO QTY]])" & Chr(10) & " +IF([#[RDS-ALC QTY]]="""",0,[#[RDS-ALC QTY]])" & Chr(10) & " +IF([#[RDS-JOB QTY]]="""",0,[#[RDS-JOB QTY]])" & Chr(10) & " +IF([#[RDS-GIT QTY]]="""",0,[#[RDS-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AO11" & ":AO" & NumRowsRDS)
'CompanyP W/H 1
NumRows1 = GetLastRow("AX11", "AX11" & VBA.CStr(StrtPOs))
Range("AX11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""P1""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[P1-PO QTY]]="""",0,[#[P1-PO QTY]])" & Chr(10) & " +IF([#[P1-ALC QTY]]="""",0,[#[P1-ALC QTY]])" & Chr(10) & " +IF([#[P1-JOB QTY]]="""",0,[#[P1-JOB QTY]])" & Chr(10) & " +IF([#[P1-GIT QTY]]="""",0,[#[P1-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AX11" & ":AX" & NumRows1)
'CompanyP W/H DS
NumRowsPDS = GetLastRow("BG11", "BG11" & VBA.CStr(StrtPOs))
Range("BG11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R02""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[PDS-PO QTY]]="""",0,[#[PDS-PO QTY]])" & Chr(10) & " +IF([#[PDS-ALC QTY]]="""",0,[#[PDS-ALC QTY]])" & Chr(10) & " +IF([#[PDS-JOB QTY]]="""",0,[#[PDS-JOB QTY]])" & Chr(10) & " +IF([#[PDS-GIT QTY]]="""",0,[#[PDS-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("BG11" & ":BG" & NumRowsPDS)

Inserting formula as string into a cell

It seems to be a problem with some characters in it
My original formula in the worksheet (working):
ThisWorkbook.Worksheets("Zazmluvnenia").Cells("3", "AM").Value = "=Výkony!M4 & ", " & TEXT(Výkony!J4;"d.m.yyyy") & ", " & TEXT(Výkony!K4;"hh:mm")"
Putting this into VBA doesn't work because of the quotes. I tried to solve the problem this way: How do I put double quotes in a string in vba?
I changed my code according to the answers on the question above:
"=Výkony!M3 & " & Chr(34) & ", " & Chr(34) & " & TEXT(Výkony!J3;" & Chr(34) & "d.m.yyyy" & Chr(34) & ") & " & Chr(34) & ", " & Chr(34) & " & TEXT(Výkony!K3;" & Chr(34) & "hh:mm" & Chr(34) & ")"
And error '1004' occurred.
Double Quotes vs CHR(34)
Your CHR solution was almost fine, but you didn't know that VBA doesn't recognize the semi-colon (;) as a separator like Excel does. You always have to replace every semi-colon (used as a separator) with a comma in VBA.
Similarly VBA uses the dot (.) as the decimal separator.
The Code
Option Explicit
Sub SEMI()
Dim strF As String
' Double double-quotes ("") in several rows.
strF = "=Výkony!M4&"", """ _
& "&TEXT(Výkony!J4,""d.m.yyyy"")&"", """ _
& "&TEXT(Výkony!K4,""hh:mm"")"
' CHR(34) in several rows.
strF = "=Výkony!M4&" & Chr(34) & ", " & Chr(34) _
& "&TEXT(Výkony!J4," & Chr(34) & "d.m.yyyy" & Chr(34) & ")&" _
& Chr(34) & ", " & Chr(34) _
& "&TEXT(Výkony!K4," & Chr(34) & "hh:mm" & Chr(34) & ")"
' Double double-quotes ("") in one row.
strF = "=Výkony!M4&"", ""&TEXT(Výkony!J4,""d.m.yyyy"")&"", ""&TEXT(Výkony!K4,""hh:mm"")"
' CHR(34) in one row.
strF = "=Výkony!M4&" & Chr(34) & ", " & Chr(34) & "&TEXT(Výkony!J4," & Chr(34) & "d.m.yyyy" & Chr(34) & ")&" & Chr(34) & ", " & Chr(34) & "&TEXT(Výkony!K4," & Chr(34) & "hh:mm" & Chr(34) & ")"
ThisWorkbook.Worksheets("Zazmluvnenia").Cells("3", "AM").Value = strF
End Sub

Formula/VBA - Application defined or object defined error

I'm trying to create a vba code to write a formula in determined cell on my sheet, but it doesn't work. What's wrong ? 'Cause, i really don't see what am i doing wrong in this code.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your computer's use of a ; as the regional list separator is fouling you up. VBA is very EN-US-centric so the Range.Formula and Range.FormulaR1C1 expect a comma (,) as the function's argument list separator.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, " & """" & "DROP" & """" & ", " & """" & "REVIEW" & """" & "), " & """" & "REVIEW" & """" & "), " & """" & """" & ")"
Alternately, Range.FormulaLocal property or Range.FormulaR1C1Local property can be used with your own semi-colon as the list separator.
Range("BC" & ActiveCell.Row).FormulaLocal = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your doubling up double quotes within a quoted string is a bit verbose and xlR1C1 notation would save some steps. TEXT(,) is the same as "" in a formula; each produces the same zero-length string.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=""REVIEW"", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=""OK"", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
'.FormulaR1C1
Range("BC" & ActiveCell.Row).FormulaR1C1 = "=IF(RC54=""REVIEW"", IF(ROW(R:R)<MAX(IF(C54:C54=""OK"", C1:C1)), IF(TODAY()-RC51>=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
The xlA1 and xlR1C1 formula rewrites above each produce the following when ActiveCell is on the second row.
=IF($BB2="REVIEW", IF(ROW($BB2)<MAX(IF($BB:$BB="OK", $A:$A)), IF(TODAY()-$AY2>=3, "DROP", "REVIEW"), "REVIEW"), TEXT(,))
As I mentioned in comments above, that formula would seem to be an array formula. In that case, use the .Formula comma based rewrite but change the Range.Formula property to Range.FormulaArray.

print variable to output text file in excel 2010 vba

The below excel 2010 vba runs perfectly except I can not seem to get the variable process (time elapsed) to print from the Call Perl section to the analysis.txt, which captures some information regarding the analysis. Thank you :).
Private Sub CommandButton3_Click()
Dim MyBarCode As String ' Enter Barcode
Dim MyScan As String ' Enter ScanDate
Dim MyDirectory As String
'GET USER INPUT '
Line1:
MyBarCode = Application.InputBox("Please enter the last 5 digits of the barcode", "Bar Code", Type:=2)
If MyBarCode = "False" Then Exit Sub 'user canceled
Do
MyScan = Application.InputBox("Please enter scan date", "Scan Date", Date - 1, Type:=2)
If MyScan = "False" Then Exit Sub 'user canceled
If IsDate(MyScan) Then Exit Do
MsgBox "Please enter a valid date format. ", vbExclamation, "Invalid Date Entry"
Loop
'CREATE NEXUS DIRECTORY AND VERIFY FOLDER '
MyDirectory = "N:\1_DATA\MicroArray\NexusData\" & "2571683" & MyBarCode & "_" & Format(CDate(MyScan), "m-d-yyyy") & "\"
If Dir(MyDirectory, vbDirectory) = "" Then
MkDir MyDirectory
Else
MsgBox ("Already exsists! Please enter again")
GoTo Line1
End If
'Write to text file
Open MyDirectory & "sample_descriptor.txt" For Output As #1
Print #1, "Experiment Sample" & vbTab & "Control Sample" & vbTab & "Display Name" & vbTab & "Gender" & vbTab & "Control Gender" & vbTab & "Spikein" & vbTab & "Location" & vbTab & "Barcode" & vbTab & "Medical Record" & vbTab & "Date of Birth" & vbTab & "Order Date"
Print #1, "2571683" & MyBarCode & "_532Block1.txt" & vbTab & "2571683" & MyBarCode & "_635Block1.txt" & vbTab & ActiveSheet.Range("B8").Value & " " & ActiveSheet.Range("B9").Value & vbTab & ActiveSheet.Range("B10").Value & vbTab & ActiveSheet.Range("B5").Value & vbTab & ActiveSheet.Range("B11").Value & vbTab & ActiveSheet.Range("B12").Value & vbTab & "2571683" & MyBarCode & vbTab & ActiveSheet.Range("C201").Value & vbTab & ActiveSheet.Range("D201").Value & vbTab & ActiveSheet.Range("E201").Value
Print #1, "2571683" & MyBarCode & "_532Block2.txt" & vbTab & "2571683" & MyBarCode & "_635Block2.txt" & vbTab & ActiveSheet.Range("C8").Value & " " & ActiveSheet.Range("C9").Value & vbTab & ActiveSheet.Range("C10").Value & vbTab & ActiveSheet.Range("C5").Value & vbTab & ActiveSheet.Range("C11").Value & vbTab & ActiveSheet.Range("C12").Value & vbTab & "2571683" & MyBarCode & vbTab & ActiveSheet.Range("C202").Value & vbTab & ActiveSheet.Range("D202").Value & vbTab & ActiveSheet.Range("E202").Value
Print #1, "2571683" & MyBarCode & "_532Block3.txt" & vbTab & "2571683" & MyBarCode & "_635Block3.txt" & vbTab & ActiveSheet.Range("D8").Value & " " & ActiveSheet.Range("D9").Value & vbTab & ActiveSheet.Range("D10").Value & vbTab & ActiveSheet.Range("D5").Value & vbTab & ActiveSheet.Range("D11").Value & vbTab & ActiveSheet.Range("D12").Value & vbTab & "2571683" & MyBarCode & vbTab & ActiveSheet.Range("C203").Value & vbTab & ActiveSheet.Range("D203").Value & vbTab & ActiveSheet.Range("E203").Value
Print #1, "2571683" & MyBarCode & "_532Block4.txt" & vbTab & "2571683" & MyBarCode & "_635Block4.txt" & vbTab & ActiveSheet.Range("E8").Value & " " & ActiveSheet.Range("E9").Value & vbTab & ActiveSheet.Range("E10").Value & vbTab & ActiveSheet.Range("E5").Value & vbTab & ActiveSheet.Range("E11").Value & vbTab & ActiveSheet.Range("E12").Value & vbTab & "2571683" & MyBarCode & vbTab & ActiveSheet.Range("C201").Value & vbTab & ActiveSheet.Range("D204").Value & vbTab & ActiveSheet.Range("E204").Value
Close #1
'CREATE ANALYSIS LOG '
Dim Process As String
Open MyDirectory & "analysis.txt" For Output As #2
Print #2, "Analysis done by" & " " & Environ("UserName") & " " & "on" & " " & Date & " " & "at" & " " & Time & " " & "and took" & Process & "to complete"
Close #2
'CONFIRM ENTRIES '
Dim Patient As String
Dim Barcode As String
Dim Directory As String
Dim MyFile As Variant
Dim MyFolder As String
Dim i As Long
Directory = "N:\1_DATA\MicroArray\NexusData\" & "2571683" & MyBarCode & "_" & Format(CDate(MyScan), "m-d-yyyy")
Patient = ActiveSheet.Range("B9").Value & " " & ActiveSheet.Range("C9").Value & " " & ActiveSheet.Range("D9").Value & " " & ActiveSheet.Range("E9").Value
Barcode = "2571683" & MyBarCode
MsgBox "The patients that will be analyzed are:" + Patient & "The barcode is:" + Barcode
iYesNo = MsgBox("Do the patients and barcode match the setup sheet?", vbYesNoCancel)
Select Case iYesNo
Case vbYes
GoTo Line2
Case vbNo
MsgBox ("Doesn't match! Please enter again")
Call DeleteFolder(Directory)
GoTo Line1
End Select
'ADD VBA REFERENCE: MICROSOFT XML, v3.0 or v6.0 '
Line2:
Dim oXMLFile As New MSXML2.DOMDocument
Dim imgNode As MSXML2.IXMLDOMNodeList, destNode As MSXML2.IXMLDOMNodeList
Dim XML­File­Name As String
XML­File­Name = "C:\Users\cmccabe\Desktop\EmArray\Design\imagene.bch"
oXMLFile.Load (XML­File­Name)
'EXTRACT NODES INTO LIST AND REWRITE NODES '
Set imgNode = oXMLFile.DocumentElement.SelectNodes("/Batch/Entry/Image")
imgNode(0).Text = "I:\" & "2571683" & MyBarCode & "_532.tif"
imgNode(1).Text = "I:\" & "2571683" & MyBarCode & "_635.tif"
Set destNode = oXMLFile.DocumentElement.SelectNodes("/Batch/Entry/Destination")
destNode(0).Text = MyDirectory
destNode(1).Text = MyDirectory
'SAVE UPDATED XML '
oXMLFile.Save XML­File­Name
'UNINTIALIZE OBJECTS '
Set imgNode = Nothing
Set destNode = Nothing
Set oXMLFile = Nothing
'CALCULATE TIME FOR PROCESS '
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer ' define start time
'CALL JAVA PROGRAM USING SHELL AND COMPLETE PROCESS '
Dim wshell As Object
Set wshell = CreateObject("wscript.shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wshell.Run Chr(34) & "C:\Users\cmccabe\Desktop\EmArray\Design\java.bat", windowsStyle, waitOnReturn
'UPDATE PERL VARIABLES USING SHELL '
Dim PerlCommand As String, PerlParameters As String, VarDirectory As String
Dim var As String, var1 As String, var2 As String, var3 As String
MsgBox ("Verifying spike-ins, please wait")
VarDirectory = "N:\1_DATA\MicroArray\NexusData\" & "2571683" & MyBarCode & "_" & Format(CDate(MyScan), "m-d-yyyy")
var = VarDirectory
var1 = "sample_descriptor.txt"
var2 = "C:\cygwin\home\cmccabe\test_probes8.txt"
var3 = var & "\" & "output.txt"
'CALL PERL '
PerlCommand = """C:\Users\cmccabe\Desktop\EmArray\Design\perl.bat"""
PerlParameters = """" & var & """" & " " & _
"""" & var1 & """" & " " & _
"""" & var2 & """" & " " & _
"""" & var3 & """"
CreateObject("wscript.shell").Run PerlCommand & " " & PerlParameters, windowsStyle, waitOnReturn
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss") 'elapsed seconds
Process = MinutesElapsed & " minutes"
iYesNo = MsgBox("ImaGene and spike-in anlysis complete, do you want to run NxClinical?", vbYesNoCancel)
Select Case iYesNo
Case vbYes
'CALL AND EXECUTE NXCLINICAL '
Dim x As Variant
Dim Path As String
'SET INSTALLATION PATH '
Path = "C:\Program Files\BioDiscovery\NxClinical Client\NxClinical.exe"
x = Shell(Path, vbNormalFocus)
Case vbNo
'CLOSE AND EXIT '
Application.DisplayAlerts = False
Application.Quit
End Select
End Sub

Vim tabular alignment first column different

I’m trying to format tables in LaTeX nicely using the Tabular command, and frequently I’ll want to right-align the leftmost column, then left align everything else. Using :'<,'>Tabularize /&/r1c1l0 almost works, but only on the first column pair; the rest of the table is weirdly aligned almost as if Tabular is repeating that sequence.
How can I write a command that will align the table code without me having to type something like :'<,'>Tabularize /&/r1c1l0c1l0c1l0…?
EDIT: Example table code
\begin{longtable}{>{\bfseries}l >{\scshape}Fl -l -l -l -l -l -l -l -l -l -l}
\toprule
Case & & \SetRowStyle{\scshape} nil & 1sg & 2sg & 3sg & 1du;inc & 2du & 1pl;inc & 1pl;exc & 2pl & 3pl \\
\midrule\endhead
\multirow{4}{*}{Focal} & nat & -a & -ai & -uta & -ima & -iva & -etua & -isa & -ečé & -atá & -amia \\
& sgv & -ena & -enai & -enta & -enima & -eniva & -entua & -enisa & -enčé & -entá & -emia \\
& du & -eva & -evai & -evta & -evima & -eviva & -evtua & -evisa & -evčé & -evtá & -evmia \\
& pl & -esa & -esai & -esta & -esima & -esiva & -estua & -esisa & -esčé & -está & -esmia \\
\midrule
\multirow{4}{*}{Nominative} & nat & -am & -aim & -utam & -imam & -ium & -etum & -isam & -ečem & -atám & -amim \\
& sgv & -enam & -enaim & -entam & -enimam & -enium & -entum & -ensim & -enčem & -entám & -emim \\
& du & -evam & -evaim & -evtam & -evimam & -evium & -evtum & -evsim & -evčem & -evtám & -evmim \\
& pl & -esam & -esaim & -estam & -esimam & -esium & -estum & -essim & -esčem & -estám & -esmim \\
\midrule
\multirow{4}{*}{Absolutive} & nat & -aš & -aiš & -utaš & -imaš & -iuš & -etuš & -isaš & -ečeš & -atáš & -amiš \\
& sgv & -enaš & -enaiš & -entaš & -enimaš & -eniuš & -entuš & -ensiš & -enčeš & -entáš & -emiš \\
& du & -evaš & -evaiš & -evtaš & -evimaš & -eviuš & -evtuš & -evsiš & -evčeš & -evtáš & -evmiš \\
& pl & -esaš & -esaiš & -estaš & -esimaš & -esiuš & -estuš & -essiš & -esčeš & -estáš & -esmiš \\
\bottomrule
\caption{Consonant-final animate noun suffixes\label{tab:nst_animate_consonant_stem_suffixes}}
\end{longtable}
It compiles just fine but I’d like the source code to be formatted somewhat nicer.
You may find vim-easy-align useful.
Try
:'<,'>EasyAlign&mrl*
which is a shorthand notation for
:'<,'>EasyAlign & { 'mode_sequence': 'rl*' }
& tells the plugin to align text using predefined & rule for LaTeX tables
rl* means right, left, left, left, ...
You can adjust left and right margin around the delimiter as follows:
:'<,'>EasyAlign&mrl*l0r1
a shorthand notation for
:'<,'>EasyAlign & { 'mode_sequence': 'rl*', 'left_margin': 0, 'right_margin': 1 }

Resources